如何在Mysql中跨多个列查找重复值?
我有一张这样的桌子 我想检查列A和列B中的所有行,并获得重复的计数 例如,我想得到如何在Mysql中跨多个列查找重复值?,mysql,database-design,count,duplicates,Mysql,Database Design,Count,Duplicates,我有一张这样的桌子 我想检查列A和列B中的所有行,并获得重复的计数 例如,我想得到 count of 12 as 3(2 times in A+1 time in B) count of 11 as 2(2 times in A+0 time in B) count of 13 as 2(1 time in A+0 time in B) 我如何实现它?您可以通过一个union all计算总发生次数。where子句只能显示A列中出现的值: select nr ,
count of 12 as 3(2 times in A+1 time in B)
count of 11 as 2(2 times in A+0 time in B)
count of 13 as 2(1 time in A+0 time in B)
我如何实现它?您可以通过一个
union all
计算总发生次数。where
子句只能显示A
列中出现的值:
select nr
, count(*)
from (
select A as nr
from YourTable
union all
select B
from YourTable
) sub
where nr in -- only values that occur at least once in the A column
(
select A
from YourTable
)
group by
nr
having count(*) > 1 -- show only duplicates
您可以组合A和B中的所有值,然后按进行分组。 然后只选择A列中的值
Select A, count(A) as cnt
From (
Select A
from yourTable
Union All
Select B
from yourTable) t
Where t.A in
(select distinct A from yourTable)
Group by t.A
Order by t.A;
结果:
A cnt
11 2
12 3
13 1
请参见演示:我只想获得一个columns条目。此查询返回两列sanwer edited,以将结果限制为
A
列中出现的值。这就是你想要的吗?@Andomar,我想你应该加上COUNT(*)大于1,因为OP只需要重复的hanks。我使用手机,所以格式化非常有限