I have 3 tables: Products, ProductProperties, Properties. I need to get all products, which do not have “Property B”. Tables:
# Products # Join table # Properties
+----+-----------+ +------------+-------------+ +-------------+---------------+
| id | name | | product_id | property_id | | property_id | property_name |
+----+-----------+ +------------+-------------+ +-------------+---------------+
| 1 | Product 1 | | 1 | 1 | | 1 | Propeprty A |
| 2 | Product 2 | | 1 | 2 | | 2 | Propeprty B |
| 3 | Product 3 | | 2 | 1 | +-------------+---------------+
+----+-----------+ | 2 | 2 |
| 3 | 1 |
+------------+-------------+
In this particular case I expect Product 3 to be returned.
Is it possible to get all required products within a single db query? What the smallest query possible to achieve that?
Edited. Queries with sub-queries considered bad.
Seems like you want an OUTER JOIN and then a WHERE clause to bring back the non-matched Properties. This is untested, but: