How to split comma separated string into rows in mysql?

When we have a comma-separated string like 1,2,3... and if we need to display as row like 

ID 

Here is the query we can use for the same 

e.g. 

Table name: tblTest 

Column Name : vals 


SELECT 
     DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(vals, ',', n.digit+1), ',', -1) val 
FROM 
     tblTest 
     INNER JOIN (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) n ON LENGTH(REPLACE(vals, ',' , '')) <= LENGTH(vals)-n.digit; 


Hope this is helpful!

Comments

Post a Comment

Popular posts from this blog

Error: The client and server cannot communicate, because they do not possess a common algorithm.

Disable Copy Paste And Mouse Right Click JavaScript, jQuery