本文给大家介绍有关数据库SQL递归查询在不同数据库中的实现方法,具体内容请看下文。
比如表结构数据如下:
Table:Tree
ID Name ParentId
1 一级 0
2 二级 1
3 三级 2
4 四级 3
SQL SERVER 2005查询方法:
- //上查
- with tmpTree
- as
- (
- select * from Tree where Id=2
- union all
- select p.* from tmpTree inner join Tree p on p.Id=tmpTree.ParentId
- )
- select * from tmpTree
- //下查
- with tmpTree
- as
- (
- select * from Tree where Id=2
- union all
- select s.* from tmpTree inner join Tree s on s.ParentId=tmpTree.Id
- )
- select * from tmpTree
新闻热点
疑难解答