我需要將父列中的父子記錄放入其他具有該ID的表中。和 我嘗試這樣做:如何在sql中的一列插入父和子記錄?
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
謝謝您的回答@saj ..如果可能的話沒有 'UNION ALL' 選項 – Meline