读书人

两个表中的同一个字段怎么查询剔除重复

发布时间: 2012-03-25 20:55:16 作者: rapoo

两个表中的同一个字段如何查询剔除重复?
两个表中都有UserName字段,现在需要两个表中所有的UserName,如何做?

SQL code
select distinct UserName from table1union allselect distinct UserName from table2


这个不能剔除两个合并以后重复的,要怎么解决?

[解决办法]
select distinct UserName from table1
union
select distinct UserName from table2
[解决办法]
select UserName from table1
union
select UserName from table2
--返回两个表的所有的UserName ,且去掉重复的
[解决办法]
SQL code
select UserName from table1union  select UserName from table2
[解决办法]
把 all 去掉
[解决办法]
SQL code
select UserName from tbl1union  select UserName from tbl2--union all 是不去重的 

读书人网 >SQL Server

热点推荐