2012-04-03 76 views
0

为什么这回23列(适量):为什么添加计数()到我的SQL查询停止它的工作?

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; 

干杯。

+0

这将是最好的显示一些示例数据? – cctan 2012-04-03 01:09:09

回答

3

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

+0

感谢您的答案,但我将如何分组?大声笑 – 2012-04-03 01:11:01

+0

@AndyLobel使用GROUP BY – 2012-04-03 01:21:30

+0

@AmitBhargava好,如果我把组通过comments.id在最后它返回适量的行,但计数()没有,所以我不知道 – 2012-04-03 01:56:49

相关问题