I have this SQl query;
SELECT prod.* FROM bb_admin.bb_directory_products as prod
LEFT JOIN bb_admin.bb_map_product_category as mapcat ON prod.product_id = mapcat.product_id
LEFT JOIN bb_admin.bb_categories_products as cat ON mapcat.category_id = cat.category_id
LEFT JOIN bb_admin.bb_map_product_tag as maptag ON prod.product_id = maptag.product_id
LEFT JOIN bb_admin.bb_tags as tag ON maptag.tag_id = tag.tag_id
WHERE (prod.status='1' OR prod.status='3' OR prod.status='5')
AND prod.is_catalogue='1' AND cat.slug = 'barongs-suits'
AND tag.title IN ('gray','classic')
GROUP BY prod.product_id
What I want is to display products with tags 'gray' and 'classic'.
I tried AND tag.title = 'gray' AND tag.title = 'classic'
but its not working well.
Thanks!
SELECT prod.* FROM bb_admin.bb_directory_products as prod LEFT JOIN bb_admin.bb_map_product_category as mapcat ON prod.product_id = mapcat.product_id LEFT JOIN bb_admin.bb_categories_products as cat ON mapcat.category_id = cat.category_id LEFT JOIN bb_admin.bb_map_product_tag as maptag ON prod.product_id = maptag.product_id LEFT JOIN bb_admin.bb_tags as tag ON maptag.tag_id = tag.tag_id WHERE (prod.status='1' OR prod.status='3' OR prod.status='5') AND prod.is_catalogue='1' AND cat.slug = 'barongs-suits' AND tag.title IN ('gray','classic') GROUP BY prod.product_id HAVING count(distinct tag.title) = 2
Add a having
clause that makes sure a product_id
has both tags.