Mysql
 sql >> Database >  >> RDS >> Mysql

Selezione della frequenza di un risultato che potrebbe apparire in più colonne (SQL)

Ecco un approccio:

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
;

Quante righe ci sono nella tabella?