Split values in parts with sqlite

前端 未结 2 1427
南笙
南笙 2021-01-13 03:09

I\'m struggling to convert

a | a1,a2,a3
b | b1,b3
c | c2,c1

to:



        
2条回答
  •  借酒劲吻你
    2021-01-13 03:50

    Yes, a recursive common table expression is the solution:

    with x(one, firstone, rest) as 
    (select one, substr(many, 1, instr(many, ',')-1) as firstone, substr(many, instr(many, ',')+1) as rest from data where many like "%,%"
       UNION ALL
     select one, substr(rest, 1, instr(rest, ',')-1) as firstone, substr(rest, instr(rest, ',')+1) as rest from x    where rest like "%,%" LIMIT 200
    )
    select one, firstone from x UNION ALL select one, rest from x where rest not like "%,%" 
    ORDER by one;
    

    Output:

    a|a1
    a|a2
    a|a3
    b|b1
    b|b3
    c|c2
    c|c1
    

提交回复
热议问题