我有一个mysql表
Table A
--------------------
item_id category_id
--------------------
1 1
1 2
1 4
2 1
2 3
想要一个sql查询,它将选择数组中的所有匹配项
示例: 的
given category_ids are 1,4 it should return only item_id 1
given category_ids are 1 it should return item_id 1 and 2
由于
答案 0 :(得分:0)
对于类别1,4:
SELECT item_id, COUNT(*) c
FROM TableA
WHERE category_id IN (1, 4)
GROUP BY item_id
HAVING c = 2
对于第1类:
SELECT item_id, COUNT(*) c
FROM TableA
WHERE category_id IN (1)
GROUP BY item_id
HAVING c = 1
我认为您应该能够看到模式 - HAVING
子句应该与类别数匹配。
这假设item_id, category_id
在表格中是唯一的。
答案 1 :(得分:-1)
SELECT item_id,category_id
FROM TableA
WHERE category_id IN(1,4)
SELECT item_id,category_id
FROM TableA
WHERE category_id IN(1)