当前位置: 代码迷 >> Sql Server >> 查询两表不同记录,该怎么解决
  详细解决方案

查询两表不同记录,该怎么解决

热度:0   发布时间:2016-04-27 15:21:13.0
查询两表不同记录
例:
如:tableA                                      
    id           name                 age                    
    1               aa                   22            
    2               bb                   33          
    3               cc                   42          
    4               dd                   55  
    5               gg                   32  
    6               ff                   88  
       
    tableB      
    id           name                 age          
    1               aa                   22      
    2               bb                   33          
    3               cc                   44          
    4               dd                   55      
    5               ee                   66      
    6               ff                   77      
       
    希望查出结果:      
          cc                  
          gg                  
          ee                        
          ff          
就是说两个表中只要有一个字段不同就取出它的name不要重复的.谢谢...

------解决方案--------------------
select
a.name
from
tableA a
where
exists(select 1 from tableB where name=a.name and (age!=a.age or id!=a.id))
or
not exists(select 1 from tableB where name=a.name)
union
select b.name from tableB b where not exists(select 1 from tableA where name=b.name)
------解决方案--------------------
select distinct isnull(A.name,B.name)
  相关解决方案