我有一张这样的桌子
mysql - 如何在Mysql的多个列中查找重复值?-LMLPHP
我要检查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)

我怎么能忍受呢?

最佳答案

您可以合并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

查看演示:http://sqlfiddle.com/#!9/9fcfe9/3

关于mysql - 如何在Mysql的多个列中查找重复值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49511885/

10-16 17:52