日期:2014-05-18  浏览次数:20562 次

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

select distinct UserName from table1
union all
select 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 table1
union  
select UserName from table2

------解决方案--------------------
把 all 去掉
------解决方案--------------------
SQL code

select UserName from tbl1
union  
select UserName from tbl2
--union all 是不去重的