I have three tables, let's say they are fruit, fruit_data and data. A row in the fruit table may have 0-n data references, and these are made using the fruit_data table:
fruit fruit_data data
----------------------------------------------
| fruit_id | fruit_id | data_id |
| ... | data_id | name |
| ... | | ... |
I'd like to select all fruit.fruit_ids that do not have a reference to a data row where data.name is "glass".
I've come up with:
SELECT DISTINCT fruit.fruit_id
FROM fruit
JOIN fruit_data ON fruit_data.fruit_id = fruit.fruit_id
JOIN data ON data.data_id = fruit_data.data_id
WHERE data.name != 'glass';
but this seems to exclude fruit rows that do not have any references to data.
So, how to write a query that returns all fruit_ids that either don't have any data rows or that only have data rows where data.name != "glass"?