mysql - SQL split values to multiple rows -


i have table :

id | name     1  | a,b,c     2  | b 

i want output :

id | name     1  |     1  | b     1  | c     2  | b 

if can create numbers table, contains numbers 1 maximum fields split, use solution this:

select   tablename.id,   substring_index(substring_index(tablename.name, ',', numbers.n), ',', -1) name   numbers inner join tablename   on char_length(tablename.name)      -char_length(replace(tablename.name, ',', ''))>=numbers.n-1 order   id, n 

please see fiddle here.

if cannot create table, solution can this:

select   tablename.id,   substring_index(substring_index(tablename.name, ',', numbers.n), ',', -1) name   (select 1 n union    select 2 union select 3 union    select 4 union select 5) numbers inner join tablename   on char_length(tablename.name)      -char_length(replace(tablename.name, ',', ''))>=numbers.n-1 order   id, n 

an example fiddle here.


Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -