计算数据库中的重复值

时间:2012-03-13 16:33:07

标签: mysql sql

示例数据库:

userid / value
887 / x
887 / y 
2313 / x
2313 / s
123 / e
2313 / i

我如何得到以下回复?

2313 / 3
887 / 2
123 / 1

3 个答案:

答案 0 :(得分:4)

select userid, count(*)
from table_name
group by userid

编辑:正如anothershrubbery所说,如果你关心结果的顺序那么:

select userid, count(*) as frequency
from table_name
group by userid
order by frequency desc

答案 1 :(得分:1)

group by与汇总功能count()

一起使用

答案 2 :(得分:0)

如果您要检查每个用户ID的值数,可以这样做:

select userid, count(value) as total
from table_name
group by userid
order by total desc

如果您尝试检查每个用户ID的 distinct 值,您可以这样做:

select userid, count(distinct value) as total
from table_name
group by userid
order by total desc