首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-23 
查询两表不同记录例:如:tableAidnameage1aa222bb333cc424dd555gg326ff88tableBidnameage1aa222bb333cc444d

查询两表不同记录
例:
如: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)


from A
full join B on isnull(A.name, ' ')=isnull(B.name, ' ')
where isnull(A.age,0) <> isnull(B.age,0)

热点排行