Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - merge two tables content in one table/view

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:

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

like image 974
BeNdErR Avatar asked Apr 30 '13 13:04

BeNdErR


People also ask

How do I combine data from multiple tables into one table in SQL?

SQL JOIN. A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

How do you create a view that combines data from multiple tables?

A view that combines data from multiple tables enables you to show relevant information in multiple tables together. You can create a view that combines data from two or more tables by naming more than one table in the FROM clause.


2 Answers

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
like image 125
Bhavin Chauhan Avatar answered Sep 22 '22 02:09

Bhavin Chauhan


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)

then just:

select * from allcountries;
like image 32
Paul Roub Avatar answered Sep 20 '22 02:09

Paul Roub