递归查询应该从某个地方开始(因为现在您要在第一个子查询中选择整个表)。我建议从最后一张唱片开始
order
列为null并遍历到第一条记录:
with recursive team(id, ord) as (values(53,55),(55,52),(52,54),(54,null)),
teams as (
select *, 1 as depth from team where ord is null -- select the last record here
union all
select t.*, ts.depth + 1 as depth
from team t join teams ts on ts.id = t.ord) -- note that the JOIN condition reversed comparing to the original query
select * from teams order by depth desc; -- finally reverse the order
ââââââ¬âââââââ¬ââââââââ
â id â ord â depth â
ââââââ¼âââââââ¼ââââââââ¤
â 53 â 55 â 4 â
â 55 â 52 â 3 â
â 52 â 54 â 2 â
â 54 â ââââ â 1 â
ââââââ´âââââââ´ââââââââ