gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:06:11 25 4
gpt4 key购买 nike

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

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

非常感谢!

最佳答案

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。

关于Mysql 查询还必须显示没有投票的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14306014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com