我有2个表post
和comments
,我想选择10个与所有评论相关联的帖子:
SELECT * from post p left join comments c on p.id = c.post LIMIT 10;
此查询不起作用,因为限制是针对带有评论的帖子而不是仅针对帖子。那怎么办呢?是否可以不使用内部查询?
答案 0 :(得分:0)
试试这个
SELECT P.*,C.*
From (SELECT * FROM post LIMIT 0,10) AS P
Left Join comments C ON C.Id = P.ID
答案 1 :(得分:0)
直接的解决方案是分开一点:
SELECT *
FROM comments c
LEFT JOIN (SELECT * FROM post LIMIT 10) p
ON p.id = c.post;