SQL SQL追加表查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3331932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:56:42  来源:igfitidea点击:

SQL Append table queries

sql

提问by Dave

I have two tables, say table1 with two rows of data say row11 and row12 and table2 with 3 rows of data sat row21, row22, row23

我有两个表,比如 table1 有两行数据,比如 row11 和 row12,table2 有 3 行数据,分别是 row21、row22、row23

Can anyone provide me with the SQL to create a query that returns

任何人都可以向我提供 SQL 以创建返回的查询

row11
row12
row21
row22
row23

Note: I dont want to create a new table just return the data.

注意:我不想创建一个新表只是返回数据。

回答by OMG Ponies

Use UNION ALL, based on the example data:

使用UNION ALL,基于示例数据:

SELECT * FROM TABLE1
UNION ALL
SELECT * FROM TABLE2

UNIONremoves duplicates - if both tables each had a row whose values were "rowx, 1", the query will return one row, not two. This also makes UNIONslower than UNION ALL, because UNION ALLdoes not remove duplicates. Know your data, and use appropriately.

UNION删除重复项 - 如果两个表都有一行,其值为“rowx, 1”,则查询将返回一行,而不是两行。这也UNION比 慢UNION ALL,因为UNION ALL不会删除重复项。了解您的数据,并适当使用。

回答by Ned Batchelder

select * from table1 union select * from table2

回答by Myles J

Why not use a UNION?

为什么不使用 UNION?

SELECT Col1,Col2,Col3 FROM TABLE1

从表 1 中选择 Col1、Col2、Col3

UNION

联盟

SELECT Col1,Col2,Col3 FROM TABLE2

从 TABLE2 中选择 Col1、Col2、Col3

Are the columns on the two tables identical?

两个表上的列是否相同?

回答by agcala

In MS Accessyou can achieve the same goal with an INSERT INTOquery:

MS Access 中,您可以通过INSERT INTO查询实现相同的目标:

INSERT INTO TABLE1 SELECT * FROM TABLE2;