怎么样才能用SQL把我两张表中重复的内容删除掉

来源:百度知道 编辑:UC知道 时间:2024/05/26 06:57:07
我有两张表。一张table1,另一张table2
其中,table1和table2中有相同的内容,我怎么样才能把两张表合并成一张。而且没有重复内容呢?
我的表大概是这样的:
table1
13312345678
134xxxxxxxx
135xxxxxxxx
136xxxxxxxx
13312345678
......
table2
137xxxxxxxx
138xxxxxxxx
139xxxxxxxx
13312345678
......
我希望变成
13312345678
134xxxxxxxx
135xxxxxxxx
136xxxxxxxx
137xxxxxxxx
138xxxxxxxx
......
我想要完整的语句。谢谢!

SELECT * FROM Table1
UNION
SELECT * FROM Table2
下面的是包含重复的行
SELECT * FROM Table1
UNION ALL
SELECT * FROM Table2
---------------讲得特别详细了,呵呵

说一点,C表肯定不能有相同名字的列
下面的语句是按你的意图,把生成的数据插入到C表,其实就是利用了full outer join

select identity(int,1,1) as id,isnull(a.单位,0) as 单位1,isnull(a.姓名,0) as 姓名1,isnull(b.单位,0) as 单位2,isnull(b.姓名,0) as 姓名2
into [C表]
from test01 a full join test02 b
on a.单位=b.单位 and a.姓名=b.姓名

SELECT * FROM Table1
UNION
SELECT * FROM Table2
下面的是包含重复的行
SELECT * FROM Table1
UNION ALL
SELECT * FROM Table2

用SQL代码