gpt4 book ai didi

sql - 在postgresql中获取一个 child 的所有 parent

转载 作者:行者123 更新时间:2023-11-29 13:12:00 26 4
gpt4 key购买 nike

我在 SQL Server 上运行成功。我的系统在 SQL Server 和 postgresql 上运行。

但是当我从 SQL Server 更改为 postgresql 时,它运行失败。

我的sql是这样的:

with name_tree as 
(
SELECT DepartNo, ParentNo FROM Departments
WHERE DepartNo IN (
SELECT DepartNo FROM BelongToDepartment
WHERE UserNo = 1)
union all
select C.DepartNo, C.ParentNo
from Departments c
join name_tree p on C.DepartNo = P.ParentNo
AND C.DepartNo<>C.ParentNo
)
select * from name_tree

错误是:

name_tree doesn't exist

如何在postgresql中获取一个 child 的所有 parent

MSQ 服务器中的更多详细信息 here

最佳答案

尝试:

with RECURSIVE name_tree as 
(
SELECT DepartNo, ParentNo FROM Departments
WHERE DepartNo IN (
SELECT DepartNo FROM BelongToDepartment
WHERE UserNo = 1)
union all
select C.DepartNo, C.ParentNo
from Departments c
join name_tree p on C.DepartNo = P.ParentNo
AND C.DepartNo<>C.ParentNo
)
select * from name_tree

关于sql - 在postgresql中获取一个 child 的所有 parent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53553389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com