2013-08-05 49 views
0

我有这个SQl查询;如何在MySQL中过滤两个标签参数的结果?

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 

我想要的是显示带'灰色'和'经典'标签的产品。

我试过AND tag.title = 'gray' AND tag.title = 'classic',但它不能正常工作。

谢谢!

回答

1
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

添加having条款,确保一个product_id具有标签。

+0

@iamnards:是否解决了您的问题? –

+0

谢谢!有用! – iamnards