select b.no,b.eno,c.tname from b,c where b.typeno=2 and b.status='U' and c.id=b.id and b.no not exists 
(select 1 from a where a.state!='D' and a.id=b.id and b.no=a.no) order by b.eno

解决方案 »

  1.   

    更优化(如果是CBO:)->
    select b.no,b.eno,c.tname from b,c where b.no not exists 
    (select 1 from a where a.state!='D' and a.id=b.id and b.no=a.no) 
    and c.id=b.id and
    and b.typeno=2 and b.status='U' 
    order by b.eno
      

  2.   

    更优化(如果是CBO:)->
    select b.no,b.eno,c.tname from b,c where b.no not exists 
    (select 1 from a where a.state!='D' and a.id=b.id and b.no=a.no) 
    and c.id=b.id and
    and b.typeno=2 and b.status='U' 
    order by b.eno
      

  3.   

    不好意思,前些时候上不了网select b.no,b.eno,c.tname from b,c where b.typeno=2 and b.status='U' and c.id=b.id and b.no not exists 
    (select 1 from a where a.state!='D' and a.id=b.id and b.no=a.no) order by b.eno
    好象要把b.no去了才行?
    select b.no,b.eno,c.tname from b,c where b.typeno=2 and b.status='U' and c.id=b.id and not exists 
    (select 1 from a where a.state!='D' and a.id=b.id and b.no=a.no) order by b.eno
    ????是不是这样呢?
      

  4.   

    这样最优化。搞不懂为什么一定用not exists   not in
    select b.no,b.eno,c.tname 
    from b,c,a
    where b.typeno=2 and b.status='U' and c.id=b.id 
    and b.no =a.no 
    and (a.state!='D' or a.id=b.id)
    order by b.eno
      

  5.   

    不好意思,应该是
    select b.no,b.eno,c.tname 
    from b,c,a
    where b.typeno=2 and b.status='U' and c.id=b.id 
    and b.no =a.no 
    and (a.state='D' or not(a.id = b.id))
    order by b.eno
      

  6.   

    用not exists比用not in可以快很多  --这句话抱相反意见
    若表间数据量小,那not in比not exists快
    要以测试为准
      

  7.   

    用not exists比用not in可以快很多  --这句话抱相反意见
    若表间数据量小,那not in比not exists快
    要以测试为准
      

  8.   

    用not exists比用not in可以快很多  --这句话抱相反意见
    若表间数据量小,那not in比not exists快
    要以测试为准
      

  9.   

    IN和EXISTS
    有时候会将一列和一系列值相比较。最简单的办法就是在where子句中使用子查询。在where子句中可以使用两种格式的子查询。
    第一种格式是使用IN操作符:
    ... where column in(select * from ... where ...); 
    第二种格式是使用EXIST操作符:
    ... where exists (select 'X' from ...where ...); 
    我相信绝大多数人会使用第一种格式,因为它比较容易编写,而实际上第二种格式要远比第一种格式的效率高。在Oracle中可以几乎将所有的IN操作符子查询改写为使用EXISTS的子查询。
    第二种格式中,子查询以‘select 'X'开始。运用EXISTS子句不管子查询从表中抽取什么数据它只查看where子句。这样优化器就不必遍历整个表而仅根据索引就可完成工作(这里假定在where语句中使用的列存在索引)。相对于IN子句来说,EXISTS使用相连子查询,构造起来要比IN子查询困难一些。
    通过使用EXIST,Oracle系统会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle系统在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因。
    同时应尽可能使用NOT EXISTS来代替NOT IN,尽管二者都使用了NOT(不能使用索引而降低速度),NOT EXISTS要比NOT IN查询效率更高。