The following command selects all the items from ‘stock’ that appear in ‘items’ with the correct orderRef.
SELECT a.* FROM stock a LEFT JOIN items b ON a.id = b.stockId WHERE b.orderRef='orderRef'
This works, but I need all the items that aren’t listed in table ‘items’ with the correct orderRef.
I thought I should change ‘ON’ to its opposite, but ‘OFF’ didn’t work.
The best way to do a “not in” query is MySQL is the following:
The way the MySQL optimizers works, the
not existsperforms best. This can be further enhanced by having an index on items.stockID.Note that when you use a left outer join, you may inadvertently be multiplying the nubmer of rows, if there are duplicates in the second table.