1

有这三个表:

  1. 帖子
  2. post_replies
  3. 喜欢

这个查询正在返回我的帖子,他们的回复都很好。

SELECT posts.title, posts.num, posts.status, posts.category, posts.content, posts.member_num, COUNT( posts_replies.blyrb_num ) AS count, COUNT( likes.comment_num ) AS likes_count
FROM posts_replies
INNER JOIN posts ON ( posts_replies.blyrb_num = posts.num )
LEFT JOIN likes ON ( likes.comment_num = posts_replies.num )
WHERE posts.status <>3
AND posts.access = 'Public'
AND (
posts.title LIKE '%dress%'
OR posts.content LIKE '%dress%'
)
GROUP BY posts.num

但问题是这个查询只返回有一些回复的帖子。我不想要这样的限制,我想要所有的帖子,不管他们有没有回复。

关于我在这里做错了什么的任何想法?

谢谢

4

2 回答 2

2

在“FROM”和您的第一个“JOIN”中交换表格,并将您的第一个“JOIN”更改为 LEFT JOIN,应该可以解决问题:

SELECT posts.title, posts.num, posts.status, posts.category, posts.content, posts.member_num, COUNT( posts_replies.blyrb_num ) AS count, COUNT( likes.comment_num ) AS likes_count
FROM posts
LEFT JOIN posts_replies ON ( posts_replies.blyrb_num = posts.num )
LEFT JOIN likes ON ( likes.comment_num = posts_replies.num )
WHERE posts.status <>3
AND posts.access = 'Public'
AND (
posts.title LIKE '%dress%'
OR posts.content LIKE '%dress%'
)
GROUP BY posts.num
于 2013-03-03T08:53:28.397 回答
1

要做的事情:

  • 使用LEFT JOIN而不是INNER JOIN
  • 交换表格:POSTposts_replies

询问,

SELECT  posts.title, 
        posts.num, 
        posts.status, 
        posts.category, 
        posts.content, 
        posts.member_num, 
        COUNT( posts_replies.blyrb_num ) AS count, 
        COUNT( likes.comment_num ) AS likes_count
FROM    posts
        LEFT JOIN posts_replies 
            ON ( posts_replies.blyrb_num = posts.num )
        LEFT JOIN likes 
            ON ( likes.comment_num = posts_replies.num )
WHERE   posts.status <> 3 AND 
        posts.access = 'Public'
        AND 
        (
            posts.title LIKE '%dress%'
            OR posts.content LIKE '%dress%'
        )
GROUP   BY posts.num
于 2013-03-03T08:53:48.820 回答