1

我目前的查询:

SELECT *
FROM `reported_users`
GROUP BY `reported_user`
ORDER BY ???

我有一张reported_users桌子。当我网站上的用户报告另一个用户违反规则时,报告的用户将作为一行插入此表中。所以同一个用户可以在表中有多行。为了确保我只GROUP BYreported_user现场使用一次就获得每个用户。但是我怎样才能让ORDER BY被报道最多的用户到最少。因此,如果表格如下所示:

reported_user | reporter
---------------------------
Joe           | Bob
Jake          | Nady
Lisa          | Tim
Joe           | Jim
Joe           | Foo
Lisa          | Bar

查询应返回:

Joe (3)
Lisa (2)
Jake (1)

我还想返回每个用户被举报的次数。如何才能做到这一点?

4

2 回答 2

4
SELECT reported_user, count(*)
FROM reported_users
GROUP BY reported_user
ORDER BY count(*) desc
于 2012-07-13T23:43:28.053 回答
3
SELECT reported_user, count(*)
FROM reported_users
GROUP BY reported_user
ORDER BY count(*) Desc
于 2012-07-13T23:43:47.683 回答