Split comma separated value from table column into rows using mysql?



PHP Snippet 1:

  SELECT ID,SUBSTRING_INDEX(SUBSTRING_INDEX(t.CITY_ID, ',', n.n), ',', -1) value
  FROM Table1 t CROSS JOIN 
  (
   SELECT a.N + b.N * 10 + 1 n
     FROM 
    (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
   ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
    ORDER BY n 
   ) n
 WHERE n.n <= 1 + (LENGTH(t.CITY_ID) - LENGTH(REPLACE(t.CITY_ID, ',', '')))
 ORDER BY ID,value

PHP Snippet 2:

ID  VALUE
1   12
1   16
1   17
1   18
1   35
2   12
2   16

PHP Snippet 3:

DROP FUNCTION IF EXISTS `SET_COUNT`;
        
CREATE FUNCTION `SET_COUNT`($strlist MEDIUMBLOB) RETURNS SMALLINT(5) UNSIGNED
NO SQL
DETERMINISTIC
RETURN 1+CHAR_LENGTH($strlist)-CHAR_LENGTH(REPLACE($strlist,',',''));
              
DROP FUNCTION IF EXISTS `SET_EXTRACT`;
             
CREATE FUNCTION `SET_EXTRACT`($i SMALLINT UNSIGNED, $strlist MEDIUMBLOB) RETURNS VARBINARY(255)
NO SQL
DETERMINISTIC
RETURN NULLIF(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(0b0, ',', $strlist, ',', 0b0), ',', $i+1.5*(SIGN($i+0.5)+1)-1), ',', -SIGN($i+0.5)),0b0);

PHP Snippet 4:

  SELECT SET_EXTRACT(n.digit, 'a,b,c,d,f,g')AS split_value FROM  (SELECT 0 digit 
  UNION ALL SELECT 1 
  UNION ALL SELECT 2 
  UNION ALL SELECT 3  
  UNION ALL SELECT 4 
  UNION ALL SELECT 5 
  UNION ALL SELECT 6
  UNION ALL SELECT 7
  UNION ALL SELECT 8
  UNION ALL SELECT 9
  UNION ALL SELECT 10) AS  n WHERE n.digit < set_count('a,b,c,d,f,g')