Mysql Query还必须显示没有投票的帖子

时间:2013-01-13 17:17:43

标签: mysql

我遇到了mysql查询的问题,

我有这些表:上传,用户和评级。

我使用以下查询来获取我的最新帖子:

Select All
              upload.*,
              Count(ratings.id) As totalrates,
              Avg(ratings.rating) As avgrating,
              users.nickname
            From upload
              Inner Join ratings
                On ratings.upload_id1 = upload.id
              Inner Join users
                On upload.users_id = users.id
            Group By upload.id DESC

然而,当一个帖子还没有任何评级时,它就不会出现,因此无法投票:p

any1是否知道如何调整此查询或任何其他有关如何解决此问题的好建议?

非常感谢!

1 个答案:

答案 0 :(得分:1)

Select All
              upload.*,
              Count(ifnull(ratings.id,0)) As totalrates,
              Avg(ifnull(ratings.rating,0)) As avgrating,
              users.nickname
            From
              upload Inner Join
              users On upload.users_id = users.id left join
              ratings On ratings.upload_id1 = upload.id
            Group By
              upload.id DESC

两次更改

  1. 使用左连接将评分加入用户并上传
  2. 如果未找到评级,则默认为0.