我有一张表,其中列出了分布在五个不同列中的名称。我正在尝试获得6个最常见的不同名称。每个名称只会在每个记录中出现一次。这五列是name_1,name_2 … name_5。仅出于名称的缘故,将表称为“ mytable”。
任何帮助将非常感激。
这是一种方法:
SELECT name, COUNT(1) FROM ( SELECT name_1 AS name FROM mytable UNION ALL SELECT name_2 AS name FROM mytable UNION ALL SELECT name_3 AS name FROM mytable UNION ALL SELECT name_4 AS name FROM mytable UNION ALL SELECT name_5 AS name FROM mytable ) AS myunion GROUP BY name ORDER BY COUNT(1) DESC LIMIT 6 ;
表格中有几行?