sqlite 树型结构查询

前端之家收集整理的这篇文章主要介绍了sqlite 树型结构查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

表结构:tblCity(ID,ParentID,Name)

因为sqlite 没有row_number函数,也不能递归查询,所幸它有RowID 这个字段。只好采用这种 笨方法


1)

select ID,Name,1 as Level from tblCity where ParentID=0
union all 
select a.ID,a.Name,c.RowID as Level from tblCity a 
inner join tblCity b on a.ParentID=b.ID
inner join 
(
      select ParentID from tblCity group by ParentID
) c on a.ParentID=c.ParentID

2)
select ID,c.RowID as Level from tblCity a 
inner join tblCity b on a.ParentID=b.ID
inner join 
(
      select ParentID from tblCity group by ParentID
) c on a.ParentID=c.ParentID
原文链接:https://www.f2er.com/sqlite/201369.html

猜你在找的Sqlite相关文章