2013-08-05 51 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