mysql查询父子关系的语句_SQL Server 父子关系查询脚本

with nodes

as (

select * from users as child where child.id = 6

union all

select par.* from users as par

inner join nodes as rc on par.id = rc.pid

)

select * from users where id in (

select id from nodes n

)

mysql查询父子关系的语句_SQL Server 父子关系查询脚本_第1张图片

-- //

with nodes

as (

select * from users as par where par.id = 2

union all

select child.* from users as child

inner join nodes as rc on child.pid = rc.id

)

select * from users where id in (

select id from nodes n

)

mysql查询父子关系的语句_SQL Server 父子关系查询脚本_第2张图片

你可能感兴趣的:(mysql查询父子关系的语句)