使用 SQL 查询
select u.name,count(u.name) as 'followers'
from user u,follow f
where u.type = 'c' AND f.followee = u.email
group by u.name
为我的数据库中的所有用户获取正确的值,但是,没有按行分组的完全相同的查询只给我第一个值。我是第一次学习 SQL,很难弄清楚这是为什么。
最佳答案
当你使用 count without group by
时,它会计算所有记录并返回 single line
而当你使用 count with group by
时,它将根据用户的姓名对用户进行分组
,并返回每个组
的计数。
关于除非使用 group by,否则 mySQL 计数只返回一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40503132/