将具有相同字段的多个选择语句合并到单个输出中的sql是什么?



我有三个表

table_1_Top_Performers:
col_1_ID | col_2_FirstName | col_3_LastName    
1          Johny               Smith
2          John                Que
3          Kristina            Zimmerman
table_2_Mid_Performers:
col_1_ID | col_2_FirstName | col_3_LastName
1          Stewart            Joe
2          Amy                Green
3          John               Smithsonian
table_3_Bottom_Performers:
col_1_ID | col_2_FirstName | col_3_LastName    
1          Noah              Cantana
2          Abe               Jackson
3          Smith             Markson

我要找的输出是:

col_1_ID | col_2_FirstName | col_3_LastName    
1          Noah              Cantana
2          Abe               Jackson
3          Smith             Markson
1          Stewart           Joe
2          Amy               Green
3          John              Smithsonian
1          Johny             Smith
2          John              Que
3          Kristina          Zimmerman

这些可以通过一个简单的SQL语句实现吗?甚至是PL/SQL调用?

您可以使用UNION ALL生成您的输出

create table top_performers (id int,firstname nvarchar(30),lastname nvarchar(30));    
insert into top_performers(id,firstname,lastname)
values (1,'Johny','Smith'),(2,'John','Que'),(3,'Kristina','Zimmerman');
create table mid_performers (id int,firstname nvarchar(30),lastname nvarchar(30));    
insert into mid_performers(id,firstname,lastname)
values (1,'Stewart','Joe'),(2,'Amy','Green'),(3,'John','Smith');
create table bottom_performers (id int,firstname nvarchar(30),lastname nvarchar(30));
insert into bottom_performers(id,firstname,lastname)
values (1,'Noah','Cantana'),(2,'Abe','Jackson'),(3,'Smith','Markson');
select *,'top' from top_performers
union all
select *,'mid' from mid_performers
union all 
select *,'bottom' from bottom_performers

:

id  firstname   lastname    (No column name)    
1   Johny       Smith        top    
2   John        Que          top    
3   Kristina    Zimmerman    top    
1   Stewart     Joe          mid    
2   Amy         Green        mid   
3   John        Smith        mid   
1   Noah        Cantana      bottom    
2   Abe         Jackson      bottom
3   Smith       Markson      bottom

最新更新