字符串拆分 并列转行。 我已写了两篇贴子。
SQL Server 的请参见
https://www.jianshu.com/p/365496ce93d5
mysql 的请参见
https://www.jianshu.com/p/b40ac2dc3c25
今天在QQ群里有人问 sqlite要实现此功能。
发现sqlite即没有表值函数,也不能用xml 也没有mysql 的 SUBSTRING_INDEX函数
还好,可以使用公用表达式
drop table if exists test; create table test(id int,ids varchar(50)); insert into test select 1,'1,12,135' union all select 2,'2,25,234'; with split(id,splid,idsstr) as select id,'',ids||',' from test UNION ALL SELECT id,substr(idsstr, 0, instr(idsstr, ',')),substr(idsstr, instr(idsstr, ',')+1) FROM split WHERE idsstr!='' select id,splid from split where splid != '' order by id ;