SQL split values to multiple rows

前端 未结 9 1395
刺人心
刺人心 2020-11-21 05:28

I have table :

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

i want output like this :

id | name    
1  | a    
1  | b    
1  | c             


        
9条回答
  •  醉话见心
    2020-11-21 05:43

    Here is my solution

    -- Create the maximum number of words we want to pick (indexes in n)
    with recursive n(i) as (
        select
            1 i
        union all
        select i+1 from n where i < 1000
    )
    select distinct
        s.id,
        s.oaddress,
        -- n.i,
        -- use the index to pick the nth word, the last words will always repeat. Remove the duplicates with distinct
        if(instr(reverse(trim(substring_index(s.oaddress,' ',n.i))),' ') > 0,
            reverse(substr(reverse(trim(substring_index(s.oaddress,' ',n.i))),1,
                instr(reverse(trim(substring_index(s.oaddress,' ',n.i))),' '))),
            trim(substring_index(s.oaddress,' ',n.i))) oth
    from 
        app_schools s,
        n
    

提交回复
热议问题