如何在sql中的一列中插入父记录和子记录?
我需要将一列中的父子记录放到另一个具有该ID的表中。及 我试过这个:如何在sql中的一列中插入父记录和子记录?,sql,sql-server,Sql,Sql Server,我需要将一列中的父子记录放到另一个具有该ID的表中。及 我试过这个: select parent.Parent,child1.child1,Child2.child2 from parent join Child1 on child1.ParentIdId=parent.ParentID join Child2 on child1.child1Id=child2.child1Id` Create table parent (ParentID int, Parent varchar(1
select parent.Parent,child1.child1,Child2.child2
from parent
join Child1 on child1.ParentIdId=parent.ParentID
join Child2 on child1.child1Id=child2.child1Id`
Create table parent (ParentID int, Parent varchar(10))
Create table Child1 (child1Id int, child1 varchar(10), ParentIdId int)
Create table Child2 (child2Id int, child2 varchar(10), child1Id int)
insert into parent values(10,'Sony'),(20,'Apple'),(30,'HTC'),(40,'Nexus')
insert into Child1 values(100,'Sony1',10),(200,'Sony2',10),(300,'Apple1',20),(400,'Apple2',20),(500,'HTC1',30),(600,'HTC2',30),
(700,'Nexus1',40),(800,'Nexus2',40)
insert into Child2 values(1000,'Sony11',100),(2000,'Sony22',100),(3000,'Apple11',200),(4000,'Apple22',200),(5000,'HTC11',300),(6000,'HTC22',300),
(7000,'Nexus11',400),(8000,'Nexus22',400)
我需要的输出:
Ids Products Parents
10 Sony null
20 Apple null
30 HTC null
40 Nexus null
100 Sony1 10
200 Sony2 10
300 Apple1 20
400 Apple2 20
500 HTC1 30
600 HTC2 30
700 Nexus2 40
800 Nexus2 40
1000 Sony11 100
2000 Sony22 100
3000 Apple11 200
4000 Apple22 200
5000 HTC11 300
6000 HTC22 300
7000 Nexus11 400
8000 Nexus22 400
您可能必须在下面3种不同的select查询中执行此操作
SELECT ParentIds as Ids,
Parent as Products,
CAST(NULL AS INT) AS Parent
INTO #Product
FROM Parent
UNION ALL
SELECT child1.child1id,
Child1.child1,
Child1.ParentId
FROM parent
INNER JOIN Child1 on child1.ParentIdId=parent.ParentID
UNION ALL
SELECT Child2.Child2Id,child2.child2,Child2.child1Id
FROM parent
INNER JOIN Child1 on child1.ParentIdId=parent.ParentID
INNER JOIN Child2 on child1.child1Id=child2.child1Id`
您可能必须在下面3种不同的select查询中执行此操作
SELECT ParentIds as Ids,
Parent as Products,
CAST(NULL AS INT) AS Parent
INTO #Product
FROM Parent
UNION ALL
SELECT child1.child1id,
Child1.child1,
Child1.ParentId
FROM parent
INNER JOIN Child1 on child1.ParentIdId=parent.ParentID
UNION ALL
SELECT Child2.Child2Id,child2.child2,Child2.child1Id
FROM parent
INNER JOIN Child1 on child1.ParentIdId=parent.ParentID
INNER JOIN Child2 on child1.child1Id=child2.child1Id`
我认为对于您的示例,“简单联合”就足够了,但我认为您正在寻找递归cte,如下所示:
;with cte (Child, nam, parent) as
(
select * from child2
union all
select * from child1
union all
select *, null as Parent from parent
)
, cte2 as
(
select *, 0 as Levl from cte where parent is null
union all
select c1.*, c2.Levl + 1 as Levl from cte2 c2 join cte c1
on c2.child = c1.parent
)
select * from cte2
order by levl
我添加了Levl只是为了理解层次结构我认为对于您的示例,简单的联合就足够了,但我认为您正在寻找递归cte,如下所示:
;with cte (Child, nam, parent) as
(
select * from child2
union all
select * from child1
union all
select *, null as Parent from parent
)
, cte2 as
(
select *, 0 as Levl from cte where parent is null
union all
select c1.*, c2.Levl + 1 as Levl from cte2 c2 join cte c1
on c2.child = c1.parent
)
select * from cte2
order by levl
我添加Levl只是为了理解层次结构,而不使用UNIONALL和多个select语句
SELECT COALESCE(parent.ParentID, Child1.child1id, Child2.child2id),
COALESCE(Child2.child2, Child1.child1, parent.Parent),
COALESCE( Child1.ParentIdId, Child2.child1id) FROM parent
FULL JOIN Child1 on 1 = 2
FULL JOIN Child2 on 1 = 2
不使用union all和多个select语句
SELECT COALESCE(parent.ParentID, Child1.child1id, Child2.child2id),
COALESCE(Child2.child2, Child1.child1, parent.Parent),
COALESCE( Child1.ParentIdId, Child2.child1id) FROM parent
FULL JOIN Child1 on 1 = 2
FULL JOIN Child2 on 1 = 2
谢谢你的回答@saj。。如果可能,不使用“联合所有”选项,请在@saj.上搜索您的答案。。如果可能,不使用“联合所有”option@Meline这就是你的意思吗?谢谢你。但是1=2和1=2是什么呢。我可以用身份证that@Meline这就是你的意思吗?谢谢你。但是1=2和1=2是什么呢。我可以用身份证