SQLite实现字符串拆分,并列转行

字符串拆分 并列转行。 我已写了两篇贴子。
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 ;
split.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容