我有一个数据库表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
正如您在此处注意到的那样虽然我grouped by receiveduserid
.. receiveduserid 2
未分组,但在结果中多次出现..
我做错了什么?
答案 0 :(得分:1)
试试这个: 您可能需要关注此
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
第一列优先是第一列...
答案 1 :(得分:0)
选择查询语句的一部分应该包括查询语句的一部分字段。
SELECT userid,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