我在 oracle 中有以下查询。我想将其转换为 PostgreSQL 形式。有人可以帮我解决这个问题,
SELECT user_id, user_name, reports_to, position
FROM pr_operators
START WITH reports_to = 'dpercival'
CONNECT BY PRIOR user_id = reports_to;
我在 oracle 中有以下查询。我想将其转换为 PostgreSQL 形式。有人可以帮我解决这个问题,
SELECT user_id, user_name, reports_to, position
FROM pr_operators
START WITH reports_to = 'dpercival'
CONNECT BY PRIOR user_id = reports_to;
像这样的东西应该适合你(SQL Fiddle):
WITH RECURSIVE q AS (
SELECT po.user_id,po.user_name,po.reports_to,po.position
FROM pr_operators po
WHERE po.reports_to = 'dpercival'
UNION ALL
SELECT po.user_id,po.user_name,po.reports_to,po.position
FROM pr_operators po
JOIN q ON q.user_id=po.reports_to
)
SELECT * FROM q;
注意:您的设计看起来很奇怪——reports_to
包含字符串文字,但它与user_id
典型的 type 相比较integer
。