当前位置:  开发笔记 > 数据库 > 正文

使用纯SQLite将字符串拆分为行

如何解决《使用纯SQLite将字符串拆分为行》经验,为你挑选了3个好方法。

使用SQLite,我想以下面的方式分割一个字符串.

输入字符串:

C:\Users\fidel\Desktop\Temp

并让查询返回这些:

C:\
C:\Users\
C:\Users\fidel\
C:\Users\fidel\Desktop\
C:\Users\fidel\Desktop\Temp

换句话说,我想将文件路径拆分为其组成路径.有没有办法在纯SQLite中执行此操作?



1> CL...:

这可以使用递归公用表表达式:

WITH RECURSIVE split(s, last, rest) AS (
  VALUES('', '', 'C:\Users\fidel\Desktop\Temp')
  UNION ALL
  SELECT s || substr(rest, 1, 1),
         substr(rest, 1, 1),
         substr(rest, 2)
  FROM split
  WHERE rest <> ''
)
SELECT s
FROM split
WHERE rest = ''
   OR last = '\';

(你没有要求合理的方式.)



2> Lukasz Szozd..:

递归CTE:

WITH RECURSIVE cte(org, part, rest, pos) AS (
  VALUES('C:\Users\fidel\Desktop\Temp', '','C:\Users\fidel\Desktop\Temp'|| '\', 0)
  UNION ALL
  SELECT org,
         SUBSTR(org,1, pos + INSTR(rest, '\')),
         SUBSTR(rest, INSTR(rest, '\')+1),
         pos + INSTR(rest, '\')
  FROM cte
  WHERE INSTR(rest, '\') > 0                         
)
SELECT *
FROM cte
WHERE pos <> 0
ORDER BY pos; 

SqlFiddleDemo

输出:

???????????????????????????????
?            part             ?
???????????????????????????????
? C:\                         ?
? C:\Users\                   ?
? C:\Users\fidel\             ?
? C:\Users\fidel\Desktop\     ?
? C:\Users\fidel\Desktop\Temp ?
???????????????????????????????

这个怎么运作:

org  - original string does not change
part - simply `LEFT` equivalent of original string taking pos number of chars
rest - simply `RIGHT` equivalent, rest of org string
pos  - position of first `\` in the rest 

跟踪:

??????????????????????????????????????????????????????????????????????????????????????????????????
?             org              ?            part              ?           rest             ? pos ?
??????????????????????????????????????????????????????????????????????????????????????????????????
? C:\Users\fidel\Desktop\Temp  ? C:\                          ? Users\fidel\Desktop\Temp\  ?   3 ?
? C:\Users\fidel\Desktop\Temp  ? C:\Users\                    ? fidel\Desktop\Temp\        ?   9 ?
? C:\Users\fidel\Desktop\Temp  ? C:\Users\fidel\              ? Desktop\Temp\              ?  15 ?
? C:\Users\fidel\Desktop\Temp  ? C:\Users\fidel\Desktop\      ? Temp\                      ?  23 ?
? C:\Users\fidel\Desktop\Temp  ? C:\Users\fidel\Desktop\Temp  ?                            ?  28 ?
??????????????????????????????????????????????????????????????????????????????????????????????????



3> 小智..:

如果要单独搜索值,请使用以下代码:

WITH RECURSIVE split(content, last, rest) AS (
VALUES('', '', 'value1§value2§value3§value4§value5§value6§value7')
UNION ALL
  SELECT 

    CASE WHEN last = '§' 
            THEN
                substr(rest, 1, 1)
            ELSE
                content || substr(rest, 1, 1)
    END,
     substr(rest, 1, 1),
     substr(rest, 2)
  FROM split
  WHERE rest <> ''
)
SELECT 
       REPLACE(content, '§','') AS 'ValueSplit'     
FROM 
       split
WHERE 
       last = '§' OR rest ='';

结果:

**ValueSplit**

value1
value2
value3
value4
value5
value6
value7

我希望我能为遇到同样问题的人提供帮助。

推荐阅读
oDavid_仔o_880
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有