如何表示雇用的子父行?
How to represent hirerarical child-parent rows?
我有一个 persons
table 包含这些示例行:
+----+-------+-----------+
| id | name | parent_id |
+----+-------+-----------+
| 1 | max | (null) |
| 2 | payne | 1 |
| 3 | mike | 1 |
| 4 | sara | 2 |
| 7 | walt | (null) |
+----+-------+-----------+
每个人只列出一次,具有唯一的 ID
,但在 parent_id
中可以有空值。有些 child
共享相同的 parent_id
。
我还有一个tickets
table存储人物信息
+----+---------+-----------+
| id | request | person_id |
+----+---------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 3 |
| 3 | 2 | 2 |
| 4 | 2 | 3 |
| 5 | 2 | 7 |
+----+---------+-----------+
基本上,每张票可以有多个人(每个请求)。在此 table 中,我不存储 parent_id
,因为它可以从 persons
table.
中检索到
现在我尝试使用以下 SQL 语句
表示请求 #2 的 persons
层次结构
with x(id,name,parent_id)
as
(
select
p.id,p.name,p.parent_id
from
tickets t left join persons p on t.person_id = p.id
where
t.request=2
and p.parent_id is null /* for all parents */
union all
select
c.id,c.name,c.parent_id
from
tickets j left join persons c on j.person_id = c.id
join x on x.id = c.parent_id
where
j.request=2
) select * from x
但我收到此错误消息:
SQL Server Database Error: Outer join is not allowed in the recursive
part of a recursive common table expression 'x'.
我做错了什么?
在 CTE 中构建树,然后将 tickets
table 加入到树中:
with person_tree (id, name, parent_id)
as
(
select p.id, p.name, p.parent_id
from persons p
where p.parent_id is null
union all
select c.id, c.name, c.parent_id
from persons c
join person_tree p on c.parent_id = p.id
)
select *
from tickets t
left join person_tree p on t.person_id = p.id
where t.request = 2;
SQLFiddle: http://sqlfiddle.com/#!6/004df/28
我有一个 persons
table 包含这些示例行:
+----+-------+-----------+
| id | name | parent_id |
+----+-------+-----------+
| 1 | max | (null) |
| 2 | payne | 1 |
| 3 | mike | 1 |
| 4 | sara | 2 |
| 7 | walt | (null) |
+----+-------+-----------+
每个人只列出一次,具有唯一的 ID
,但在 parent_id
中可以有空值。有些 child
共享相同的 parent_id
。
我还有一个tickets
table存储人物信息
+----+---------+-----------+
| id | request | person_id |
+----+---------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 3 |
| 3 | 2 | 2 |
| 4 | 2 | 3 |
| 5 | 2 | 7 |
+----+---------+-----------+
基本上,每张票可以有多个人(每个请求)。在此 table 中,我不存储 parent_id
,因为它可以从 persons
table.
现在我尝试使用以下 SQL 语句
表示请求 #2 的persons
层次结构
with x(id,name,parent_id)
as
(
select
p.id,p.name,p.parent_id
from
tickets t left join persons p on t.person_id = p.id
where
t.request=2
and p.parent_id is null /* for all parents */
union all
select
c.id,c.name,c.parent_id
from
tickets j left join persons c on j.person_id = c.id
join x on x.id = c.parent_id
where
j.request=2
) select * from x
但我收到此错误消息:
SQL Server Database Error: Outer join is not allowed in the recursive part of a recursive common table expression 'x'.
我做错了什么?
在 CTE 中构建树,然后将 tickets
table 加入到树中:
with person_tree (id, name, parent_id)
as
(
select p.id, p.name, p.parent_id
from persons p
where p.parent_id is null
union all
select c.id, c.name, c.parent_id
from persons c
join person_tree p on c.parent_id = p.id
)
select *
from tickets t
left join person_tree p on t.person_id = p.id
where t.request = 2;
SQLFiddle: http://sqlfiddle.com/#!6/004df/28