Mysql
 sql >> Base de données >  >> RDS >> Mysql

mysql Compte plusieurs occurrences d'entrées multiplexées

Essayez cette requête

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

La sortie serait :

REASON  OCCURANCES
0           3
1           3
2           2
3           1