获取两列相同表之间的出现次数

时间:2015-05-10 20:01:06

标签: mysql sql database select count

我在如何获取MySQL中两列之间的值出现次数方面存在问题。

getdelim

我如何能够获得两列之间的出现次数,如下所示?

id1 col1 col2
2    5      3
3    3      4
4    2      1
5    1      3
6    null   2

1 个答案:

答案 0 :(得分:2)

您可以count列,然后应用SELECT val, COUNT(*) AS occurrence FROM (SELECT col1 AS val FROM mytable UNION ALL SELECT col2 AS val FROM mytable) x GROUP BY val ORDER BY occurrence DESC 聚合函数:

{=./}