我想选择与标记ID列表匹配的所有帖子。我尝试了以下方法:
SELECT * FROM posts as t WHERE (SELECT tag_id FROM post_tags WHERE post_tags.post_id = t.id) = ALL (8, 1)
这会出现以下错误:
Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '8, 1))'
如果有人知道如何解决这个问题,我会很高兴。
答案 0 :(得分:1)
SELECT p.*
FROM posts p
JOIN post_tags pt
ON pt.post_id = p.id
WHERE tag_id IN(1,8)
GROUP
BY p.id
HAVING COUNT(*) = 2
(其中' 2'等于IN()中的项目数。)