code1 | code2 | code3 | code4 | code5
row1 A B C
row2 B A D E
row3 F C
如何将此表与 codeX 列中的值计数分组 所以
A: 2
B: 2
C: 2
D: 1
E: 1
F: 1
最佳答案
如果我理解正确的话,您需要一个出现某些内容的列的列表。您可以使用 group_concat
取消数据透视表:
select code, count(*) as cnt, group_concat(which)
from (select code1 as code, 'code1' as which from table union all
select code2, 'code2' as which from table union all
select code3, 'code3' as which from table union all
select code4, 'code4' as which from table
) c
where code is not null
group by code;
我使用了完整的列名而不是#1
——它似乎更有用。但您可以为 which
输入 #1
等。
关于mysql 按多列中的值进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28432172/