select *
from a
where a.bh in (select bh from b where 这里怎么写的。

解决方案 »

  1.   

    select a.* from a
    where a.bh 
    in (select b.bh from b where b.s1<>b.shs1)
      

  2.   

    select * from a where a.bh in (select bh from b where sl<>shsl)
      

  3.   

    谢谢两位。但为什么我查出来的结果相反呢? 注意有一条记录为空
    在表B里有一条记录
    bh  sl   shsl
    5   40   Null当执行 where sl<>shsl不能得到正确的结果。加上select * from a where a.bh in (select bh from b where sl<>shsl or shsl is null)
    就搞定。
      

  4.   

    select * From A
     where A.bh in(
     select distinct bh
       from B
       where isnull(sl,0) <> isnull(shsl,0))
    说明:1、下次给出你的DBMS,以上在MS SQL Server 2000下可行;
          2、作为数据库设计,表B设计错误。
      

  5.   

    我用的是 sql server2000distinct 是啥意思
      

  6.   

    distinct使子查询中不出现重复纪录,由于IN的效率奇低,可以相对稍微提高性能,无他。
      

  7.   

    null的问题吧?select * from a where a.bh in 
    (select bh from b where sl<>shsl 
     and s1 is not null 
     and shsl is not null)没看太清楚,是不是这样?
      

  8.   

    select a1.bh,a1.name From A a1 ,( select distinct bh
                                       from B
                                       where isnull(sl,0) <> isnull(shsl,0)) a2
    where a1.bh = a2.bh;
    性能稍好一些。
      

  9.   

    是这个意思。 谢谢诸位。  我在B表有几万条记录, 我想哪种效率更高。 问题搞定。distinct使子查询中不出现重复纪录,由于IN的效率奇低,可以相对稍微提高性能,不会吧,我以前用过好多的in,nnd 是不是有更好的算法。
      

  10.   

    能不用IN就不用IN as a rule