mysql Count multiple occurrences of multiplexed entries

前端 未结 3 1137
甜味超标
甜味超标 2021-01-28 23:54

I have a mysql table as shown below,

  id          reasonCode
------      ------------------
  1           0, 1
  2           0
  3           1, 2, 3
  4                 


        
3条回答
  •  无人共我
    2021-01-29 00:59

    Try this query

    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

    Output Would be:

    REASON  OCCURANCES
    0           3
    1           3
    2           2
    3           1
    

提交回复
热议问题