代码表:tbAddress:xh(integer),country(char),town(char),village(char),主键为country+ town+ village
xh country town village
1 苏仙区 良田镇 良田村
2 苏仙区 良田镇 堆上村
3 苏仙区 坳上镇 坳上村
4 苏仙区 坳上镇 桥头村
。。
数据表:tbobject:sfid(char),csny(date),xb(char),country(char),town(char),village(char)
查找tbobject中country、town、village不是tbaddress中的值的记录

解决方案 »

  1.   


    select *
    from tbobject
    where (country + town + village) not in
          (select country + town + village from tbAddress)
      

  2.   

    select * from tbobject a where 
    not exists (select 1 from tbAddress where country+town+village=a.country+a.town+a.village)
      

  3.   


    select * from tbobject a where not exists(select 1 from tbAddress where a.country=country and a.town=town and a.village=village)
      

  4.   

    SELECT * FROM tbobject 
    WHERE  (country,town,village) NOT IN (SELECT country,town,village FROM tbAddress)
      

  5.   

    select o.*
    from tbobject o
    left outer join tbaddress s
    on o.country = s.country 
    and o.town = s.town
    and o.village = o.village
    where s.xh is null
      

  6.   


    /*
    xh country town village
    1 苏仙区 良田镇 良田村
    2 苏仙区 良田镇 堆上村
    3 苏仙区 坳上镇 坳上村
    4 苏仙区 坳上镇 桥头村
    */-->测试数据
    create table tbAddress(xh int,country varchar(20),town varchar(20),village varchar(20))
    insert into tbAddress
    select 1,'苏仙区','良田镇','良田村' union all
    select 2,'苏仙区','良田镇','堆上村' union all
    select 3,'苏仙区','坳上镇','坳上村' union all
    select 4,'苏仙区','坳上镇','桥头村'
    gocreate table tbObject(sfid varchar(10),csny datetime,xb varchar(10),country varchar(20),town varchar(20),village varchar(20))
    insert into tbObject
    select 1,'2011-1-1','a','苏仙区','良田镇','良田村' union all
    select 2,'2011-1-1','b','苏仙区','良田镇','堆上村' union all
    select 3,'2011-1-1','a','苏仙区','坳上镇','坳上村' union all
    select 4,'2011-1-1','c','苏仙区','坳上镇','桥头村' union all
    select 5,'2011-1-1','b','苏仙区','良田镇','坳上村' union all
    select 6,'2011-1-1','d','苏仙区','坳上镇','良田村' union all
    select 7,'2011-1-1','d','坳上镇','苏仙区','苏仙区' union all
    select 8,'2011-1-1','e','桥头村','坳上镇','苏仙区'
    go-- 1
    select *
    from tbObject
    where (country + town + village) not in
          (select country + town + village from tbAddress)--2
    select *
    from tbObject t
    where not exists (select 1 from tbAddress e where e.country = t.country and e.town = t.town and e.village = t.village)drop table tbAddress,tbObjectsfid       csny                    xb         country              town                 village
    ---------- ----------------------- ---------- -------------------- -------------------- --------------------
    5          2011-01-01 00:00:00.000 b          苏仙区                  良田镇                  坳上村
    6          2011-01-01 00:00:00.000 d          苏仙区                  坳上镇                  良田村
    7          2011-01-01 00:00:00.000 d          坳上镇                  苏仙区                  苏仙区
    8          2011-01-01 00:00:00.000 e          桥头村                  坳上镇                  苏仙区(4 行受影响)
      

  7.   


    --3个不同时相等
    SELECT * FROM tbobject
    WHERE country+town+village NOT IN 
    (SELECT country+town+village FROM tbAddress)--任何一个都不等
    SELECT * FROM tbobject
    WHERE country NOT IN (SELECT DISTINCT country FROM tbAddress)
    AND town NOT IN(SELECT DISTINCT  town FROM tbAddress)
    AND village NOT IN(SELECT DISTINCT  village FROM tbAddress)