Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

mysql Contar várias ocorrências de entradas multiplexadas


Tente esta consulta
SELECT Reason,COUNT(Reason) FROM
(
SELECT
  id,
  SUBSTRING_INDEX(SUBSTRING_INDEX(reasoncode, ',', n.digit+1), ',', -1) Reason
FROM
  table1
  INNER JOIN
  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) n
  ON LENGTH(REPLACE(reasoncode, ',' , '')) <= LENGTH(reasoncode)-n.digit
ORDER BY
  id,
  n.digit
) T

Group By Reason;

SQL FIDDLE

Saída seria:
REASON  OCCURANCES
0           3
1           3
2           2
3           1