我的SQLite数据库中有一个包含文件路径的列。给定文件路径的一部分,我需要返回所有下一个文件夹。我还想返回下一部分是否是字符串中的最后一个路径(或者不以'/'结尾)。所以,如果我有文件夹:
/my/folder/one
/my/folder/two
/my/folder/path/three
/another/path
/one/two/three
我有路径:
/my/folder/
结果将返回以下内容:
+----------+------+
| isLast | item |
+----------+------+
| 1 | one |
| 1 | two |
| 0 | path |
+----------+------+
我一直在努力解决这个问题,所以如果有人能提供任何指导,我们将不胜感激。
答案 0 :(得分:1)
此查询:
SELECT item NOT GLOB '*/*' AS isLast,
item
FROM (SELECT substr(MyPath, length('/my/folder/') + 1) AS item
FROM MyTable
WHERE MyPath GLOB '/my/folder/' || '*')
会给你一个这样的结果:
isLast item
------ ----
1 one
1 two
0 path/three
删除子路径需要instr()
function,这是最近才在SQLite 3.7.15中引入的:
SELECT isLast,
CASE WHEN isLast
THEN item
ELSE substr(item, 1, instr(item, '/') - 1)
END AS item
FROM (SELECT item NOT GLOB '*/*' AS isLast,
item
FROM (SELECT substr(MyPath, length('/my/folder/') + 1) AS item
FROM MyTable
WHERE MyPath GLOB '/my/folder/' || '*'))