我有这个cross_reference表,
post_id | cat_id
-----------------
3 11
-----------------
3 2
-----------------
4 5
-----------------
4 6
-----------------
4 2
-----------------
5 8
-----------------
5 13
问题是此查询不会返回任何内容....
SELECT DISTINCT post_id FROM posts_categories WHERE cat_id = 11 AND cat_id = 2
我希望它能回归'3'......
我做错了什么?
答案 0 :(得分:0)
实际上是OR
,而不是AND
。或者更好:
SELECT DISTINCT post_id FROM posts_categories WHERE cat_id IN (11,2);
虽然我希望它能回归3和4。
更新 :(根据评论)
SELECT post_id
FROM post_categories WHERE cat_id IN (2,12)
GROUP BY post_id HAVING count(*)=2;