php - 为什么将 count() 添加到我的 sql 查询中会使其无法工作?

标签 php mysql mysqli

为什么返回 23 行(正确的数量):

select users.user_id, users.fname, users.lname, 
       stars.stars, comments.comment from users 
       LEFT JOIN stars on users.user_id = stars.userid 
       JOIN comments on users.user_id = comments.sender 
       where users.user_id = ? order by comments.time desc;

这会返回 1 行?:

select users.user_id, users.fname, users.lname,
       stars.stars, count(distinct comments.id) as amount,
       comments.comment from users 
       LEFT JOIN stars on users.user_id = stars.userid 
       JOIN comments on users.user_id = comments.sender 
       where users.user_id = ? order by comments.time desc;

干杯。

最佳答案

需要对主数据进行分组或者对字段进行子查询。

关于php - 为什么将 count() 添加到我的 sql 查询中会使其无法工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9985847/

相关文章:

php - 根据先前查询的结果显示某些项目的 SQL 查询有什么问题?

php - 从错误的表中选择ID并优化mysql查询

php - htaccess重定向后从移动网站访问父网站的文件夹

php - 寻找一个快速的库来使用 PHP 呈现 PDF 文件

mysql - 当我尝试创建触发器时出错

php mysql select,数组结果内部数组结果

PHP二维数组打印索引而不是值

php - sql order by date before selection 查询帮助

PHP mysqli - 从准备好的语句返回关联数组

php - mysqli 的好处是否值得重写一个大型的工作系统?