sql with子语句的理解

在用sqlite时,用到了with子语句,记录下对其的理解 

目的:在1个字段中找到真正包含的数据,一般用于like,但该字段可能是复杂的字符串数组。

例子数据,如:

【E2806894200050010B93C473,123456】;【E2806894200050010B93C476,安全帽#04】;【E28068942000501D5D241D80,实验室10kv接地线#001】;【E28068942000502A4687958D,安全帽#06】;【E28069952000500433F08588,T2实验室0.4kv安全带#01】;【E280F3362000F00000B05094,实验室0.4kv安全帽#01】;

是由“;”分割的,分割后还有“,”分割的,所以需要对这个数据进行递归分析。

1、with子语句,生成1个临时表(a,b,c,d,...),生成后可以对其进行查询,

2、在with as (....)括号后,应该进行查询或其它操作,操作后,临时表会被删除,

3、with as ( union all select自己可形成递归数据插入临时表)查询后又加入临时表中,所以形成递归数据

with e(id,b,c) as (
select id,'',devTids from op_tools_events where id=24
union all 
select id,substr(c, 0, instr(c, ';')),substr(c, instr(c, ';')+1)from e where c!=''
)select * from e where b!=''

结果:

  可以看到b是通过第一行的c进行了递归分解得到的。

4、最后的真正查询语句:

select  id, substr(b, 2, instr(b, ',')-2) as rfid,substr(b,instr(b, ',')+1,instr(b, '】')-instr(b, ',')-1) as devcode from (
with e(id,b,c) as (
select id,'',devTids from op_tools_events where devTids like '%T2%'
union all 
select id,substr(c, 0, instr(c, ';')),substr(c, instr(c, ';')+1)from e where c!=''
)select * from e where b!=''
)
where devcode like '%T2%' order by id

结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值