php - Mysql 结果返回未分组

标签 php mysql

我有一个数据库表thanks_entry 带列

 - entryid
 - varname
 - userid
 - contenttype
 - contentid
 - dateline
 - receiveduserid

这个表存储了用户/用户点赞/感谢的数据
我想获得每月点赞/感谢次数最多的用户的统计信息

查询:

SELECT receiveduserid,COUNT(receiveduserid) AS receivedthanks 
FROM thanks_entry 
WHERE varname ='likes' AND dateline > 1420070400 
AND dateline <1422748800 
GROUP BY userid ORDER BY COUNT(receiveduserid) DESC LIMIT 20

此查询应返回按用户 ID 分组的结果 .. 但查看结果我发现

- receiveduserid|receivedthanks
 - 185883|190
 - 43455|100
 - 163068|85
 - 2|66
 - 186270|49
 - 189468|34
 - 105597|22
 - 2|17
 - 186270|17
 - 163068|16
 - 117147|15
 - 186270|15
 - 186270|14
 - 186643|13
 - 2|12
 - 189170|12
 - 118500|8
 - 2|8
 - 105597|8
 - 2|7

正如您在此处注意到的,虽然我按 receiveduserid 分组 .. receiveduserid 2 未分组并在结果中多次出现 .. 我做错了什么?

最佳答案

试试这个: 你可能想关注这个

SELECT receiveduserid,COUNT(receiveduserid) AS receivedthanks 
FROM thanks_entry 
WHERE varname ='likes' AND dateline > 1420070400 
AND dateline <1422748800 
GROUP BY receiveduserid  ORDER BY receivedthanks DESC LIMIT 20

并且您想按多列分组,然后使用 ,(逗号),例如 GROUP BY receiveduserid, another_column 第一列优先...

关于php - Mysql 结果返回未分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27996958/

相关文章:

php - OpenSSL 启用但不工作

php - php uncompress&unpack给出包含一些(10309中的9个)未定义值的数组

mysql - 更好的做法 : saving count result into field or not?

php - 如何设置 "temporary mysql session"系统

mysql - 按年和月计算总数

php - Ratchet (php) 使用类 MessageComponentInterface 和 WampServerInterface 一起使用 websockets 和推送通知

php - 当查询中传递的数据与数据库中已有的数据相同时,如何判断 MySQL 更新查询是否成功?

php - 绑定(bind)参数计数第一个参数

php - 每秒连接一个mysql数据库

python - 使用 python 连接到 SQL 数据库