33
我正在开发一个Android应用程序,它将一些数据作为树状结构存储在SQLite表中。
作为示例,表的相关部分具有以下结构:
parentId根据@tinazmu的回答进行了一些研究后,我终于提出了一个查询,似乎在SQLite中工作得很好。
WITH AncestryTree AS (
SELECT id, parentId, type
FROM Tree
WHERE parentId IS NOT NULL
UNION ALL
SELECT T.id, AT.parentId, T.type
FROM AncestryTree AT
JOIN Tree T ON AT.id = T.parentId
)
SELECT id
FROM Tree
WHERE type = 'node'
AND id NOT IN (
SELECT DISTINCT parentId
FROM AncestryTree
WHERE type = 'data'
)
这个查询可以在db-fiddle中测试,但是如果有人指出这个查询的任何缺陷或优化,那就太好了。
我可以用递归查找数据节点在每个节点的节点。第一个是SqlServer:
with treeTable as (
select *
from (values
(1,'1','node',0,null)
,(2,'2','node',1,1)
,(3,'3','node',1,1)
,(4,'4','node',2,2)
,(5,'5','node',2,3)
,(6,'6','node',2,3)
,(7,'7','node',3,4)
,(8,'8','node',3,6)
,(9,'**','data',2,2)
,(10,'**','data',2,2)
,(11,'**','data',3,4)
,(12,'**','data',3,5)
) T(id, name, type, depth, parentId)
),
Hry as (
SELECT StartNode=TN.id, ThisNode=TN.id, TN.name, tn.type, tn.depth, L=0
FROM treeTable TN
union all
SELECT StartNode=H.StartNode, ThisNode=TN.id, TN.name, TN.type, TN.depth, L=L+1
FROM treeTable TN
inner join
Hry H
on H.ThisNode=TN.ParentId
)
/* Now repeat for each 'node' node: recursively traverse each sub-tree and see if there are data nodes underneath */
select T.id
from treeTable T
left join
Hry H
on T.id=H.StartNode
and H.type='data'
where T.type='node'
and H.StartNode is null
Sqlite的
with treeTable as (
select 1 as id, '1'as name,'node' as type,0 as depth ,null as parentID union all
select 2 as id, '2'as name,'node' as type,1 as depth , 1 as parentID union all
select 3 as id, '3'as name,'node' as type,1 as depth , 1 as parentID union all
select 4 as id, '4'as name,'node' as type,2 as depth , 2 as parentID union all
select 5 as id, '5'as name,'node' as type,2 as depth , 3 as parentID union all
select 6 as id, '6'as name,'node' as type,2 as depth , 3 as parentID union all
select 7 as id, '7'as name,'node' as type,3 as depth , 4 as parentID union all
select 8 as id, '8'as name,'node' as type,3 as depth , 6 as parentID union all
select 9 as id,'**'as name,'data' as type,2 as depth , 2 as parentID union all
select 10 as id,'**'as name,'data' as type,2 as depth , 2 as parentID union all
select 11 as id,'**'as name,'data' as type,3 as depth , 4 as parentID union all
select 12 as id,'**'as name,'data' as type,3 as depth , 5 as parentID
),
Hry as (
SELECT TN.id as StartNode, TN.id as ThisNode, TN.name, tn.type, tn.depth, 0 as L
FROM treeTable TN
union all
SELECT H.StartNode as StartNode, TN.id as ThisNode, TN.name, TN.type, TN.depth, L+1 as L
FROM treeTable TN
inner join
Hry H
on H.ThisNode=TN.ParentId
)
/* Now repeat for each 'node' node: recursively traverse each sub-tree and see if there are data nodes underneath */
select T.id
from treeTable T
left join
Hry H
on T.id=H.StartNode
and H.type='data'
where T.type='node'
and H.StartNode is null
相关内容
- 使用SQL获取子节点的所有父节点
- 在树结构表中查找空节点的SQL查询
- 删除重复的XML节点SQL查询
- 迭代xml节点SQL Server上的值
- 节点sql解析器,parser.astify(sqlquery)未返回准确的输出
- 重新启动单节点 SQL-BDC 群集终结点的主机后不可用
- 在特定节点SQL Server上插入XML
- 如何在 xml 中插入动态节点?SQL 服务器
- 循环并修改XML节点SQL Server
- 节点SQL语法错误
- 节点 + SQL 列不能为空
- 如何在节点-SQL中设置SQL方言
- 节点 SQL 查询生成复杂查询
- xQuery应该为空节点(SQL Server 2008)提供适当的封闭标签
- 如何选择xml节点SQL Server
- 递归抓取父节点-SQL
- 选择所有xml排除单个特定节点SQL Server
- 转换查询创建节点(SQL Server 2008)
- postgreHow得到树的任何节点(SQL)
- 将值插入XML列的子节点Sql Server中
最新更新
- UE4 -对特定角色施加伤害
- 堆栈中的帧指针是什么?这个框架到底是什么?(汇编中的堆栈)
- 如何使string.strip()剥离多个字符?
- Git 连接问题。 "fatal: unable to connect to github.com....."
- 使用SingleStore连接两个带有整数字符串的表
- 将用户输入的值替换为React中由状态分配的值
- 如何从JSchema获取父对象名称/映射JsonObjects到新的父对象
- 为pandas数据框架列添加字符串,其中包含多个逗号分隔值
- 我试图在python中创建一个虚拟环境,但我得到错误:未能找到内置解释器python_spec=python3.5的发现
- Spark 相当于 postgres json 函数 ->
- API 无法处理我的请求,因为模板文字使 API 动态化
- 如何在VBA中引用UserForm中文本框的值?
- 获取(count(column1) + count(column2))的和
- Cypress -如何使用Cypress命令验证PDF文件中的数据
- JS reduceRight是导致数组成为对象,我怎么能修复逻辑?
- 是否有办法在SQL中创建一个临时2d数组?
- 如何使用特定列中的id在R中绑定data.frames
- 如何隐藏源代码,而我运行react-app-rewired构建和react-app-rewired启动
- React native检测设备是否是RTL
- AWS CLI "$ aws configure"创建 .awsconfig 和 .awscredentials 文件,而不是 .aws/config 和 .aws/credenatials
- 如何运行bash脚本与目标?
- 在Discord.py中发送嵌入消息的顺序
- 从列表中删除重复项,不包括开始和结束
- 从索引和值点创建矩阵
- MongoDB:在$group之后使用$setWindowFields
- 无法用聚合填充和构建文档
- 如何将应用程序池分配给不同的ASP.Net Core网站在AWS Elastic Beanstalk?
- 使用sed或awk和tcl regsub函数从字符串中删除制表符
- 如何创建带试用期的分条订阅
- 尝试使用 PHP 上传视频,但在某些视频上不断"No File Selected"
热门标签:
javascript python java c# php android html jquery c++ css ios sql mysql arrays asp.net json python-3.x ruby-on-rails .net sql-server django objective-c excel regex ruby linux ajax iphone xml vba spring asp.net-mvc database wordpress string postgresql wpf windows xcode bash git oracle list vb.net multithreading eclipse algorithm macos powershell visual-studio image forms numpy scala function api selenium