来自 android 的 SQLite 使用递归查询获取类似索引的路径
SQLite from android get path like indexes with recursive query
我有这个 table 命名项目,其中包含以下列和行。
id item_name parentId
--------------------------
1 Item 1 0
2 Item 2 1
3 Item 3 2
4 Item 4 1
5 Item 5 2
6 Item 6 3
7 Item 7 6
8 Item 8 7
我可以通过递归 CTE 测量父级行的级别,但我仍然需要获取此格式的路径:1.1.1、1.2.1 ...
这是我当前的查询:
with recursive items_cache (lvl, id, item_name, parent_id) as (
select 0 as lvl, id, item_name, parent_id
from items
where parent_id = 0
union all
select items_cache.lvl + 1, items.id, items.item_name, items.parent_id
from items_cache
join items on items_cache.id = items.parent_id
order by items_cache.lvl+1 desc, items.id
)
select
lvl,
*
from items_cache
想要的结果:
id item_name parentId lvl path_index
-------------------------------------------------
1 Item 1 0 0 1
2 Item 2 1 1 1.1
3 Item 3 2 2 1.1.1
6 Item 6 3 3 1.1.1.1
7 Item 7 6 4 1.1.1.1.1
8 Item 8 7 5 1.1.1.1.1.1
5 Item 5 2 2 1.1.2
4 Item 4 1 1 1.2
9 Item 9 0 0 2
我如何在 SQLite 中做到这一点? android运行的SQLite版本为3.21,不支持window功能
您可以使用子查询计算给定迭代中每个条目的路径索引,然后使用另一个递归 CTE 进行字符串连接:
with recursive to_r as (
select row_number() over (order by i.id) r, i.* from items i
),
cte(id, name, parent, lvl, ind) as (
select i.id, i.item_name, i.parentId, 0, (select sum(i1.parentId = i.parentId and i1.r < i.r) from to_r i1) + 1 from to_r i where i.parentId = 0
union all
select i1.id, i1.item_name, i1.parentId, i.lvl+1, (select sum(i2.parentId = i1.parentId and i2.r < i1.r) from to_r i2) + 1 from cte i join to_r i1 on i1.parentId = i.id
),
cte1(id, name, parent, lvl, ind, lvl1) as (
select i.id, i.name, i.parent, i.lvl, case when i.lvl != 0 then "1." || i.ind else i.ind end, case when i.lvl != 0 then i.lvl-1 else i.lvl end from cte i
union all
select i.id, i.name, i.parent, i.lvl, "1." || i.ind, i.lvl1-1 from cte1 i where i.lvl1 > 0
)
select id, name, parent, lvl, ind from cte1 where lvl1 = 0 order by ind;
使用递归 cte 获取每个 id
的级别,然后使用 ROW_NUMBER()
window 函数获取每个 id
在其父项下的排名,然后使用另一个递归将连接行号的 cte:
WITH
levels AS (
SELECT *, 0 lvl FROM items
UNION ALL
SELECT i.*, l.lvl + 1
FROM items i INNER JOIN levels l
ON l.id = i.parentId
),
row_numbers AS (
SELECT id, item_name, parentId, MAX(lvl) lvl,
ROW_NUMBER() OVER (PARTITION BY parentId, lvl ORDER BY id) rn
FROM levels
GROUP BY id, item_name, parentId
),
cte AS (
SELECT id, item_name, parentId, lvl, rn || '' path_index
FROM row_numbers
UNION ALL
SELECT r.id, r.item_name, r.parentId, r.lvl,
c.path_index || '.' || r.rn
FROM row_numbers r INNER JOIN cte c
ON r.parentId = c.id
)
SELECT *
FROM cte
GROUP BY id
HAVING MAX(LENGTH(path_index))
ORDER BY path_index
参见demo。
如果您的 SQLite 版本不支持 window 函数,请使用:
(SELECT COUNT(*) FROM levels l2
WHERE l2.parentId = l1.parentId AND l2.lvl = l1.lvl AND l2.id <= l1.id) rn
而不是:
ROW_NUMBER() OVER (PARTITION BY parentId, lvl ORDER BY id) rn
参见demo。
结果:
id
item_name
parentId
lvl
path_index
1
Item 1
0
0
1
2
Item 2
1
1
1.1
3
Item 3
2
2
1.1.1
6
Item 6
3
3
1.1.1.1
7
Item 7
6
4
1.1.1.1.1
8
Item 8
7
5
1.1.1.1.1.1
5
Item 5
2
2
1.1.2
4
Item 4
1
1
1.2
9
Item 9
0
0
2
我有这个 table 命名项目,其中包含以下列和行。
id item_name parentId
--------------------------
1 Item 1 0
2 Item 2 1
3 Item 3 2
4 Item 4 1
5 Item 5 2
6 Item 6 3
7 Item 7 6
8 Item 8 7
我可以通过递归 CTE 测量父级行的级别,但我仍然需要获取此格式的路径:1.1.1、1.2.1 ... 这是我当前的查询:
with recursive items_cache (lvl, id, item_name, parent_id) as (
select 0 as lvl, id, item_name, parent_id
from items
where parent_id = 0
union all
select items_cache.lvl + 1, items.id, items.item_name, items.parent_id
from items_cache
join items on items_cache.id = items.parent_id
order by items_cache.lvl+1 desc, items.id
)
select
lvl,
*
from items_cache
想要的结果:
id item_name parentId lvl path_index
-------------------------------------------------
1 Item 1 0 0 1
2 Item 2 1 1 1.1
3 Item 3 2 2 1.1.1
6 Item 6 3 3 1.1.1.1
7 Item 7 6 4 1.1.1.1.1
8 Item 8 7 5 1.1.1.1.1.1
5 Item 5 2 2 1.1.2
4 Item 4 1 1 1.2
9 Item 9 0 0 2
我如何在 SQLite 中做到这一点? android运行的SQLite版本为3.21,不支持window功能
您可以使用子查询计算给定迭代中每个条目的路径索引,然后使用另一个递归 CTE 进行字符串连接:
with recursive to_r as (
select row_number() over (order by i.id) r, i.* from items i
),
cte(id, name, parent, lvl, ind) as (
select i.id, i.item_name, i.parentId, 0, (select sum(i1.parentId = i.parentId and i1.r < i.r) from to_r i1) + 1 from to_r i where i.parentId = 0
union all
select i1.id, i1.item_name, i1.parentId, i.lvl+1, (select sum(i2.parentId = i1.parentId and i2.r < i1.r) from to_r i2) + 1 from cte i join to_r i1 on i1.parentId = i.id
),
cte1(id, name, parent, lvl, ind, lvl1) as (
select i.id, i.name, i.parent, i.lvl, case when i.lvl != 0 then "1." || i.ind else i.ind end, case when i.lvl != 0 then i.lvl-1 else i.lvl end from cte i
union all
select i.id, i.name, i.parent, i.lvl, "1." || i.ind, i.lvl1-1 from cte1 i where i.lvl1 > 0
)
select id, name, parent, lvl, ind from cte1 where lvl1 = 0 order by ind;
使用递归 cte 获取每个 id
的级别,然后使用 ROW_NUMBER()
window 函数获取每个 id
在其父项下的排名,然后使用另一个递归将连接行号的 cte:
WITH
levels AS (
SELECT *, 0 lvl FROM items
UNION ALL
SELECT i.*, l.lvl + 1
FROM items i INNER JOIN levels l
ON l.id = i.parentId
),
row_numbers AS (
SELECT id, item_name, parentId, MAX(lvl) lvl,
ROW_NUMBER() OVER (PARTITION BY parentId, lvl ORDER BY id) rn
FROM levels
GROUP BY id, item_name, parentId
),
cte AS (
SELECT id, item_name, parentId, lvl, rn || '' path_index
FROM row_numbers
UNION ALL
SELECT r.id, r.item_name, r.parentId, r.lvl,
c.path_index || '.' || r.rn
FROM row_numbers r INNER JOIN cte c
ON r.parentId = c.id
)
SELECT *
FROM cte
GROUP BY id
HAVING MAX(LENGTH(path_index))
ORDER BY path_index
参见demo。
如果您的 SQLite 版本不支持 window 函数,请使用:
(SELECT COUNT(*) FROM levels l2
WHERE l2.parentId = l1.parentId AND l2.lvl = l1.lvl AND l2.id <= l1.id) rn
而不是:
ROW_NUMBER() OVER (PARTITION BY parentId, lvl ORDER BY id) rn
参见demo。
结果:
id | item_name | parentId | lvl | path_index |
---|---|---|---|---|
1 | Item 1 | 0 | 0 | 1 |
2 | Item 2 | 1 | 1 | 1.1 |
3 | Item 3 | 2 | 2 | 1.1.1 |
6 | Item 6 | 3 | 3 | 1.1.1.1 |
7 | Item 7 | 6 | 4 | 1.1.1.1.1 |
8 | Item 8 | 7 | 5 | 1.1.1.1.1.1 |
5 | Item 5 | 2 | 2 | 1.1.2 |
4 | Item 4 | 1 | 1 | 1.2 |
9 | Item 9 | 0 | 0 | 2 |