map { $form->{$_} = $ref->{$_} } keys %{ $ref };
$query = <<SQL;
- SELECT b.*,
- ( EXISTS(SELECT i.bin_id FROM inventory i WHERE i.bin_id = b.id LIMIT 1)
- OR EXISTS(SELECT p.bin_id FROM parts p WHERE p.bin_id = b.id LIMIT 1))
- AS in_use
- FROM bin b
- WHERE b.warehouse_id = ?
+ SELECT b.*, use.in_use
+ FROM bin b
+ LEFT JOIN (
+ SELECT DISTINCT bin_id, TRUE AS in_use FROM inventory
+ UNION
+ SELECT DISTINCT bin_id, TRUE AS in_use FROM parts
+ ) use ON use.bin_id = b.id
+ WHERE b.warehouse_id = ?;
SQL
$form->{BINS} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));