且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL合并计数多列

更新时间:2023-02-02 22:41:54

您可以使用全部联合取消数据集的透视,然后进行聚合:

You can use union all to unpivot your dataset, and then aggregation:

select disease, count(*) total
from (
    select disease from mytable
    union all select additional_disease1 from mytable
    union all select additional_disease2 from mytable
    union all select additional_disease3 from mytable
    union all select additional_disease4 from mytable
) t
group by disease
order by total desc, disease