-1

I have a table like this and consider
table name as sample

   info | values
  -------------
   1     1,2,4,5
   2     5, 6,7

When i select the sample table with info value as 1 instead of getting comma separated value is there any way to split the value and return it as multiple rows

    info | values
     1      1
     1      2
     1      4
     1      5 

If I get this I will take this as subquery and pass it to where in cluase to fetch other data from other table.

Surya Prakash Tumma
  • 2,153
  • 4
  • 27
  • 47

1 Answers1

1

If you know the maximum number, you can use a bunch of union alls. For your sample data, this is sufficient:

select col1, substring_index(col2, ',', 1)
from t
union all
select col1, substring(substring_index(col2, ',', 2), ',', -1)
from t
where col2 like '%,%'
union all
select col1, substring(substring_index(col2, ',', 3), ',', -1)
from t
where col2 like '%,%,%'
union all
select col1, substring(substring_index(col2, ',', 4), ',', -1)
from t
where col2 like '%,%,%,%';
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786