我有两个表:users
和 comments
,其中每个评论都属于一个用户。
用户
id user class
1 mike A
2 joe B
3 john A
评论
id user_id comment
1 1 "some text"
2 2 "some text"
3 1 "some text"
4 2 "some text"
5 3 "some text"
6 2 "some text"
7 2 "some text"
我想计算每个用户类别中的评论数。 结果应该是:
A 3
B 4
我已经试过了:
SELECT users.class, count(*) AS count
FROM comments
GROUP BY comments.user_id.class
ORDER BY count DESC;
但不起作用。
最佳答案
你需要像这样加入
Select u.class, count(*)
From users u
Join comments c on c.user_id = u.id
Group by u.class
关于MySQL count, group by 和 join 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27677483/