一个表里导入了两批数据,根据batch这个字段来确定他们的批次。
我现在想通过c1和c2这两个字段比较确定这个表里发生了变化的数据记录。
两批数据通过c1这个字段关联,根据c2字段的变化来确定这条记录是否变化。
每批数据c1有重复值,我通过下面的语句能找出两批数据相同的记录:
select * from Table where c1 in 
(
  select  distinct a.c1 from 
    (select distinct c1,c2 from t_Table where batch='1' ) a,
    (select distinct c1,c2 from t_Table where batch='2' ) b 
     where (a.c1=b.c1 and a.c2<>b.c2 )
)
这个结果是正确的;但我改成下面的语句(把in改成not in)想查询不同的记录:
select * from Table where c1 not in 
(
  select  distinct a.c1 from 
    (select distinct c1,c2 from t_Table where batch='1' ) a,
    (select distinct c1,c2 from t_Table where batch='2' ) b 
     where (a.c1=b.c1 and a.c2<>b.c2 )
)
结果总是出不来请问这个问题改怎么解决?在下不胜感激了!

解决方案 »

  1.   

    提示什么????
    有时候加了distinct 返回的值也不一定就是唯一的,如果是这样的话,试着用rowid为条件试一试
      

  2.   

    首先看
     select     distinct   a.c1   from   
            (select   distinct   c1,c2   from   t_Table   where   batch='1'   )   a, 
            (select   distinct   c1,c2   from   t_Table   where   batch='2'   )   b   
              where   (a.c1=b.c1   and   a.c2 <> b.c2   ) 
    出来的结果select   *   from   Table   where   c1   not   in (上面的结果集);
    你就发现你写的sql不是你想要的结果了
      

  3.   

    上面的是有问题,我主要想问的是用not in 
    如果后面的数据量很大,用not in查询起来就很慢
    怎么换个语句来解决这个问题?
      

  4.   

    第一个要求:
    select   *   from   Table   where   c1   in  
    (
        select     distinct   a.c1   from  
            (select   distinct   c1,c2   from   t_Table   where   batch='1'   )   a,
            (select   distinct   c1,c2   from   t_Table   where   batch='2'   )   b  
              where   (a.c1=b.c1   and   a.c2 <> b.c2   )
    ) 第二个要求:
    select   *   from   Table   where   c1   in  
    (
        select     distinct   a.c1   from  
            (select   distinct   c1,c2   from   t_Table   where   batch='1'   )   a,
            (select   distinct   c1,c2   from   t_Table   where   batch='2'   )   b  
              where   (a.c1=b.c1   and   a.c2 = b.c2   )
      

  5.   

    还有LZ写出来的SQL和自己想要表达的意思,是不是相反了?〉我通过下面的语句能找出两批数据相同的记录:
    〉select   *   from   Table   where   c1   in  
    〉(
    〉   select     distinct   a.c1   from  
    〉        (select   distinct   c1,c2   from   t_Table   where   batch='1'   )   a,
    〉        (select   distinct   c1,c2   from   t_Table   where   batch='2'   )   b  
    〉          where   (a.c1=b.c1   and   a.c2 <> b.c2   )
    〉) 
    这个时候,你得到的是两批数据相同的数据?
    你的batch1 和 batch2里面,是不是存在同样的c1,但是出现多条C2,
    batch1和batch2的c2之间,既有相同的,又有不同的?我看你好像迷惑的是这里。那这个时候,你又想要什么呢?
      

  6.   

    -----------------------------------------
    不好意思,是我把SQL搞反了
    我的意思是这样
    select   *   from   Table   where   c1   not   in   

        select     distinct   a.c1   from   
            (select   distinct   c1,c2   from   t_Table   where   batch='1'   )   a, 
            (select   distinct   c1,c2   from   t_Table   where   batch='2'   )   b   
              where   (a.c1=b.c1   and   a.c2 = b.c2   ) 

    找出不同的数据的话很难查出来
    a.c2 = b.c2   
      

  7.   

    你的batch1   和   batch2里面,是不是存在同样的c1,但是出现多条C2,
    batch1和batch2的c2之间,既有相同的,又有不同的? 给出一部分数据看看呢?