-1

I have a table like this:

code allValues
--------------
C001 1,2,3,4
C002 3,4,5
C003 6,7

and I need to obtain a table like this:

code Value
----------
C001 1
C001 2
C001 3
C001 4
C002 3
C002 4
C002 5
C003 6
C003 7

Ca I do it with a SQL statement? I'm using MySQL in PHPMyAdmin, unaware of which specific database engine.

tic
  • 4,009
  • 15
  • 45
  • 86

1 Answers1

1

If you have a look up table for each "allvalue", then you can do:

select t.code, lu.value
from t join
     lookup lu
     on find_in_set(lu.value, t.allvalues) > 0;

Another method -- if you know the maximum length of allvalues -- is to do string manipulation:

select t.code,
       substring_index(substring_index(t.allvalues, ',', n.n), ',', -1) as vaue
from t cross join
     (select 1 as n union all select 2 union all select 3 union all select 4
     ) n
     on n.n <= length(t.allvalues) - length(replace(t.allvalues, ',', ''));

This version assumes the maximum length is "4", as in the question.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786