SQL - 在一个表/视图中合并两个表内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16301144/
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 15:02:51  来源:igfitidea点击:

SQL - merge two tables content in one table/view

sqldatabasesqliteview

提问by BeNdErR

I need to create a view (or a table) that holds n row values, taken from two different tables that have the same structure. For example:

我需要创建一个包含 n 行值的视图(或表),这些值取自具有相同结构的两个不同表。例如:

table Europe

表 欧洲

id    name        Country
----------------------------
1     Franz       Germany
2     Alberto     Italy
3     Miguel      Spain

table USA

表美国

id    name        Country
----------------------------
1     John        USA
2     Matthew     USA

The merged view has to be like this:

合并后的视图必须是这样的:

table WORLD

餐桌世界

id    name        Country
----------------------------
1     John        USA
2     Matthew     USA
1     Franz       Germany
2     Alberto     Italy
3     Miguel      Spain

it's possible? if it is, how?

这是可能的?如果是,如何?

Thanks in advance for your help, best regards

预先感谢您的帮助,最好的问候

回答by Bhavin Chauhan

if you just want to result than try union query

如果你只是想结果而不是尝试联合查询

SELECT id,name,Country FROM dbo.Europe
UNION
SELECT id,name,Country FROM dbo.USA

回答by Paul Roub

You can create a reusable view of the union like so:

您可以像这样创建联合的可重用视图:

create view allcountries as select * from usa union select * from world;

(name it anything you like in place of allcountries)

(把它命名为你喜欢的任何东西来代替allcountries

then just:

然后只是:

select * from allcountries;