2014-10-03 78 views
0

我想选择与标记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

ALL(8,1)?这是什么? LIMIT呢? – deadman 2014-10-03 13:13:33

回答

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()中的项数)。

+0

谢谢:)完美的工作 – Benedikt 2014-10-03 13:31:53