将具有相同字段的多个 select 语句合并到单个输出中的 sql 是什么?
What is the sql for multiple select statements with identical fields being merged into a single output?
我有三张表
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
我有三张表
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