假设有表A(id int,col1 int,col2 int,value int)
表B(col1 int,col2 int)现在要查询出表A中A.col1<>B.col1 or A.col2<>B.col2 的数据我的sql:
select * from A where id not in (select id from A,B where A.col1=B.col1 and A.col2=B.col2)有什么效率更高的写法吗?谢谢大家了PS:表中符合A.col1=B.col1 and A.col2=B.col2的数据比不符合的多很多

解决方案 »

  1.   

    估计没有了。还有一种写法:select * from A where not exists (select 1 from A,B where A.col1=B.col1 and A.col2=B.col2)但高手说IN和EXISTS的效率是一样的,楼主可以测试一下,哪条快用那条。
      

  2.   

    有人说过exists会比in快,也有人说过一样,我自己测试感觉是差不多的。
      

  3.   

    现在要查询出表A中A.col1<>B.col1 or A.col2<>B.col2 的数据我的sql:
    select * from A where id not in (select id from A,B where A.col1=B.col1 and A.col2=B.col2)好像有点问题,条件和sql语句!!!!
      

  4.   

    还应该有个条件说明什么情况下:A.col1<>B.col1 or A.col2<>B.col2就象JOIN一样,如果没有联接条件,就是全联接了。
      

  5.   

    如果不是ID一个主键,而是id1和id2的联合主键呢?请高手赐教
      

  6.   

    如A
    id1 id2 col1 col2 value
    1 1 1 2 1
    1 2 3 4 2
    1 3 3 5 3
    2 1 2 4 4
    2 2 1 5 5B:
    col1 col2
    1 2
    2 4则结果为
    id1 id2 col1 col2 value
    1 2 3 4 2
    1 3 3 5 3
    2 2 1 5 5