I’m building a database for a small bookshop with a PHP web interface. For this problem I want to display all records in the ‘products’ table (prod_core) without those that have a value in the products field ‘barcode’ equal to the ‘description’field in the ‘sales’ table (inc_core).
I actually managed to get this working for the first ‘sales’ mutation, but whenever I add a second record to the ‘sales’ table, the query doesn’t work any more. (except for the first record added). This is my SQL code:
SELECT prod_core.prodID, prod_core.title, prod_core.location, prod_core.genre,
prod_core.price, prod_core.barcode
FROM prod_core
WHERE prod_core.barcode NOT IN (
SELECT `description`
FROM inc_core
GROUP BY prod_core.prodID)
I suppose it has something to do with SQL not looping (it stops searching the table for records that match the requirements) but I’m not sure. I would very much appreciate any help!
I would suggest a
LEFT JOINinstead of aNOT IN:Also, since none of the columns in your
SELECTstatement are part of an aggregate function, I removed theGROUP BYand replaced it withDISTINCT… however, I don’t see why this table should include duplicates.EDIT: I misread your query and though that the
group bywas on the outer query, and not on the sub-query… +1 to the other answers that caught this.