Combining two tables with SQL

2019-09-09 16:49发布

Supose you have two tables with the exactly the same columns.

Table1:

Name   Type    AveSls
A       2       20
B       4       10
C       1       15

Table2:

Name   Type    AveSls
D       2       8
E       3       15
F       1       12

How do i combine the two tables in SQL server 2008 with a SQL satement so that the combined table looks like this:

Table3:

 Name    Type   AveSls
  A       2       20
  B       4       10
  C       1       15
  D       2       8
  E       3       15
  F       1       12

3条回答
爷的心禁止访问
2楼-- · 2019-09-09 17:23

You need to use the UNION operator. it's very simple to use:

SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

See the following useful links:

  1. SQL UNION Operator
  2. Introduction and Example of UNION and UNION ALL
查看更多
干净又极端
3楼-- · 2019-09-09 17:28

You can simply use UNION ALL (to get all rows even if they repeat in both tables) or UNION to get non-repeating rows.

SELECT name, 
       type, 
       avesls 
FROM   table1 
UNION ALL 
SELECT name, 
       type, 
       avesls 
FROM   table2 

Read more about UNION on MSDN.

查看更多
女痞
4楼-- · 2019-09-09 17:31

You can use,

SELECT * FROM TABLE1
UNION ALL
SELECT * FROM TABLE2
查看更多
登录 后发表回答