数据库
首页 > 数据库> > Sqlite:遍历字符串字段的子字符串

Sqlite:遍历字符串字段的子字符串

作者:互联网

我有一个包含一串数字的字段

 "2002 2005 2001 2006 2008 2344"

最好我们假设空格是分割线,否则,假设每个空格长度为4个字符.

我可以选择一个子字符串:

SELECT substr(years,1,4)  FROM TABLE 

但是不知道如何检查每个.我试图找到包含最接近0的数字的行.

我可以按BYDER和LIMIT 1订购.

解决方法:

这看起来很丑陋,但是它在一个SQLlite语句中选择了您需要的所有内容.如果假设日期在当前时间间隔内,例如1900..2100,您也可以进行一些优化.在这种情况下,您可以先剪切2个选择B,C(B:1联合2)(C:9联合0联合1)

 select years2,years from 
    (
    select i1||i2||i3||i4  as years2 from (select '0' as i1 
                   union all 
                   select '1' as i1
                   union all 
                   select '2' as i1
                   union all 
                   select '3' as i1
                   union all 
                   select '4' as i1 
                   union all 
                   select '5' as i1 
                   union all 
                   select '6' as i1
                   union all 
                   select '7' as i1
                   union all 
                   select '8' as i1
                   union all 
                   select '9' as i1) B, 
    (select '0' as i2 
                   union all 
                   select '1' as i2
                   union all 
                   select '2' as i2
                   union all 
                   select '3' as i2
                   union all 
                   select '4' as i2 
                   union all 
                   select '5' as i2 
                   union all 
                   select '6' as i2
                   union all 
                   select '7' as i2
                   union all 
                   select '8' as i2
                   union all 
                   select '9' as i2) C,
    (select '0' as i3 
                   union all 
                   select '1' as i3
                   union all 
                   select '2' as i3
                   union all 
                   select '3' as i3
                   union all 
                   select '4' as i3 
                   union all 
                   select '5' as i3 
                   union all 
                   select '6' as i3
                   union all 
                   select '7' as i3
                   union all 
                   select '8' as i3
                   union all 
                   select '9' as i3) D,
                  (select '0' as i4 
                   union all 
                   select '1' as i4
                   union all 
                   select '2' as i4
                   union all 
                   select '3' as i4 
                   union all 
                   select '4' as i4 
                   union all 
                   select '5' as i4 
                   union all 
                   select '6' as i4
                   union all 
                   select '7' as i4
                   union all 
                   select '8' as i4
                   union all 
                   select '9' as i4) E
    ) YearsAll

   left join YearsTable on (YearsTable.years like '%'||YearsAll.years2||'%')

    where YearsTable.years is not null

    order by years2 limit 1

标签:sqlite,for-loop,substring,sql,android
来源: https://codeday.me/bug/20191201/2077381.html