来自android的SQLite使用递归查询获取类似索引的路径

问题描述

我有这个表命名项目,其中包含以下列和行。

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,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,所以不支持窗口函数。

解决方法

您可以使用子查询来计算给定迭代中每个条目的路径索引,然后使用字符串连接与另一个递归 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,(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,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,"1." || i.ind,i.lvl1-1 from cte1 i where i.lvl1 > 0
)
select id,ind from cte1 where lvl1 = 0 order by ind;
,

使用递归 cte 获取每个 id 的级别,然后使用 ROW_NUMBER() 窗口函数获取每个 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,parentId
  ),cte AS (
    SELECT id,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 版本不支持窗口函数,请使用:

(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 项目 1 0 0 1
2 项目 2 1 1 1.1
3 项目 3 2 2 1.1.1
6 项目 6 3 3 1.1.1.1
7 项目 7 6 4 1.1.1.1.1
8 项目 8 7 5 1.1.1.1.1.1
5 项目 5 2 2 1.1.2
4 项目 4 1 1 1.2
9 项目 9 0 0 2

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...