1.Oracle 数据库
<span style="font-family:Microsoft YaHei;font-size:18px;">--tb 树形结构表
--id 主键
--pid 父ID
-- id=1 为查询条件 可以自定义
select * from tb start with id=1 connect by prior pid = id
</span>2.Sql Server 数据库
<span style="font-family:Microsoft YaHei;font-size:18px;">-- tb 树形结构表
-- pid 父ID
-- id=@id 为查询条件 可以自定义
with cte as
(
select * from tb where id=@id
union all
select a.* from tb a join cte b on a.id=b.pid
)
select * from cte </span>
本文介绍如何在Oracle和SqlServer中使用特定语法查询树形结构表的数据。对于Oracle, 使用START WITH和CONNECT BY PRIOR子句;对于SqlServer, 则采用通用表表达式(CTE)进行递归查询。
137

被折叠的 条评论
为什么被折叠?



