Sql server 2012 将多个表合并为一个大表
我想将相同格式(列)的多个表合并到一个大表中,这样我就可以在所有数据的并集上运行查询 例如: 表1:Sql server 2012 将多个表合并为一个大表,sql-server-2012,Sql Server 2012,我想将相同格式(列)的多个表合并到一个大表中,这样我就可以在所有数据的并集上运行查询 例如: 表1: Product Spec AProduct 1 ASpec 1 AProduct 2 ASpec 2 表2: Product Spec BProdcut 1 BSpec 1 BProduct 2 BSpec 2 表3: Product Spec CProduct 1 CSpec 1 CProduct 2 CSpec 2 想要一个大
Product Spec
AProduct 1 ASpec 1
AProduct 2 ASpec 2
表2:
Product Spec
BProdcut 1 BSpec 1
BProduct 2 BSpec 2
表3:
Product Spec
CProduct 1 CSpec 1
CProduct 2 CSpec 2
想要一个大表来显示:
Product Spec
AProduct 1 ASpec 1
AProduct 2 ASpec 2
BProdcut 1 BSpec 1
BProduct 2 BSpec 2
CProduct 1 CSpec 1
CProduct 2 CSpec 2
创建一个新表
CREATE TABLE newtable LIKE table1;
插入表1至表3中的日期
INSERT INTO newtable SELECT * FROM table1;
INSERT INTO newtable SELECT * FROM table2;
INSERT INTO newtable SELECT * FROM table3;
现在您可以使用新表了
TSQL的示例:
select * from (
select Product, Spec
from Table1
union
select Product, Spec
from Table2
union
select Product, Spec
from Table3 ) as large_table
或者像往常一样创建一个视图并查询它。
使用“union all”组合所有行,使用“union”仅组合唯一行(即,将从结果集中删除重复行)。您使用的是什么RDBMS?您使用两个不同的RDBMS标记了您的问题。请去掉不相关的标签。在你的回答中,你应该指出UNION和UNION ALL的区别,只是为了完整