为什么在我的sql查询中添加count()会阻止它工作?

时间:2012-04-03 01:03:33

标签: 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;

干杯。

1 个答案:

答案 0 :(得分:3)

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