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

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