树形结构表查询,只用一条SQL语句查询所有父级ID
表字段:ID,Tname,Pid
Pid是上级ID
现在通过ID查询出所有父级ID。
只用一条SQL语句。不能用存储过程、函数
面试时他要求只要说出使用到的关键字就行了。
[解决办法]
- SQL code
;with cte as(select * from tb where id=@idunion allselect a.* from tb a join cte b on a.id=b.pid)select * from cte
发布时间: 2012-03-28 15:40:03 作者: rapoo
树形结构表查询,只用一条SQL语句查询所有父级ID
表字段:ID,Tname,Pid
Pid是上级ID
现在通过ID查询出所有父级ID。
只用一条SQL语句。不能用存储过程、函数
面试时他要求只要说出使用到的关键字就行了。
[解决办法]
;with cte as(select * from tb where id=@idunion allselect a.* from tb a join cte b on a.id=b.pid)select * from cte