-3
id | name
100 A
101 B
102 C
產品分類表:
id | product_id | category_id
1 100 7
2 100 5
3 101 7
4 101 2
5 102 7
6 102 10
我怎樣才能得到的product_id,它的CATEGORY_ID已經得到7和2?
id | name
100 A
101 B
102 C
產品分類表:
id | product_id | category_id
1 100 7
2 100 5
3 101 7
4 101 2
5 102 7
6 102 10
我怎樣才能得到的product_id,它的CATEGORY_ID已經得到7和2?
要讓有2類和7個產品都那麼你可以這樣做
select p.*
from products p
join ProductCategory c on(p.id = c.product_id)
where c.category_id in(2,7)
group by p.id
having count(distinct c.category_id) = 2
爲了有有2類或7,則產品你可以這樣做
select distinct p.*
from products p
join ProductCategory c on(p.id = c.product_id)
where c.category_id in(2,7)
如何我可以得到product_id,它的category_id有7和2嗎?
SELECT product_id
FROM ProductCategory
WHERE category_id IN (2, 7)
GROUP BY product_id
HAVING count(DISTINCT category_id) = 2
我在sql新對不起:/我試了它只是因爲原因 – MarsPeople 2014-09-30 19:39:18