Hello guys I have a problem with a query and left join
I have 2 tables products and characteristics
I know some required characteristics with id 1,3,6
SELECT * FROM products
LEFT JOIN characteristics ...
WHERE characteristics_id IN (1,2,6)
But this not works for me because I need the products with ALL of this characteristics, not only one.
Really I need
SELECT * FROM products
INNER JOIN characteristics as c1 ... AND c1.id=1
INNER JOIN characteristics as c2 ... AND c2.id=2
INNER JOIN characteristics as c2 ... AND c2.id=3
...
But it don’t like me, there must be a simpler and efficient
Thank you.
First of all, you don’t need a
LEFT JOINat all.Second, your first attempt seems perfectly valid and I think it’s the most efficient (when there are indexes on the tables to be joined). But I’m adding another way (that seems more simple as it has only 1 join).
Simple (to add more than 3 characteristics):
More efficient (in most cases, in other words: test, test and test again in your tables with various sizes and number of ids):