MySQL:输出数组作为单独的行

时间:2017-06-27 01:46:18

标签: mysql

这是我的阵列:

set @fruit = 'apples,oranges,passion fruit,bananas,pineapples';

如何将它们输出到单独的行中,即:

apples
oranges
passion fruit
bananas
pineapples

感谢。

1 个答案:

答案 0 :(得分:0)

正如我对this question的回答,是创建函数和/或临时表的替代方法:

SELECT @i
     , substring(@string, @start, @end-@start) str
   FROM <BigTable>
     , ( SELECT @string := 'apples,oranges,passion fruit,bananas,pineapples'
              , @start := 0
              , @end := 0
              , @i := 0
              , @len := length(@string)
              , @n := @len-length(replace(@string,',',''))+1
       ) t
   WHERE (@i := @i+1) <= @n
     AND (@start := @end+1)
     AND (@end := if((@loc:=locate(',',@string,@start))=0,@len+1,@loc))