我有以下包含2列的数据库:
posts id category id
1 2
2 1
1 3
3 3
3 2
4 5我需要一个查询,当提取所有的帖子id是在类别2和3,这意味着:帖子id: 1和3。
发布于 2012-12-03 23:04:03
要选择类别2和3中的所有帖子id:
SELECT posts_id
FROM posts
WHERE categoryId IN (2, 3)
GROUP BY posts_id
HAVING COUNT(distinct categoryId) = 2;要仅选择仅与category_id = 5关联而不与任何其他关联的帖子id:
SELECT posts_id
FROM posts
GROUP BY posts_id
HAVING SUM(categoryId <> 5) = 0;发布于 2012-12-03 23:26:19
SELECT post_id, category_id
FROM posts
WHERE category_id IN (2, 3)
GROUP BY post_id
ORDER BY category_id;https://stackoverflow.com/questions/13685872
复制相似问题