如何计算MySQL中非唯一的值组合?

时间:2009-05-22 00:02:17

标签: sql mysql

我有一张包含一些遗留数据的表格,我怀疑这些数据可能有些混乱。这是一个多对多的连接表。

LIST_MEMBERSHIPS
----------------
list_id
address_id

我想运行一个查询,它会计算每个list_id - address_id对的出现次数,并显示从最高到最低出现次数的每次出现次数。

我知道它必须涉及COUNT()GROUP BY,对吧?

2 个答案:

答案 0 :(得分:4)

select list_id, address_id, count(*) as count
from LIST_MEMBERSHIPS
group by 1, 2
order by 3 desc

您可能会发现添加

很有用
having count > 1

答案 1 :(得分:1)

select count(*), list_id, address_id
from list_membership
group by list_id, address_id
order by count(*) desc