0

我的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

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

非常感谢!

4

1 回答 1

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. 使用 Left Join 将评分加入用户和上传
  2. 如果未找到评级,则默认为 0。
于 2013-01-13T17:21:17.293 回答