第一个答案:select [name],class,c.cno from card as c inner join ( select b.bno,b.cno from borrow as b inner join
( select bno,bname from books where bname='水浒')as a on (b.bno=a.bno) ) as b on(b.cno=c.cno)第二个答案
select * from card c where exists(select * from borrow as b,books a where a.bname='水浒' and b.bno=a.bno and b.cno=c.cno)我发现结果一样,但是exists感觉用起来很简洁,不知道如果在百万条数据以上,是否会影响性能?

解决方案 »

  1.   


    --
    select * from card c where exists(select * from borrow as b,books a where a.bname='水浒' and b.bno=a.bno and b.cno=c.cno)
    --改成下面这样效率更快
    select * from card c where exists(select 1 from borrow as b,books a where a.bname='水浒' and b.bno=a.bno and b.cno=c.cno)
      

  2.   

    是可以的,我的问题是 exists也可以解决(第二个答案)不知道如果在百万条数据以上的时候EXISTS用法,是否会影响性能?
      

  3.   

    如果只查一表数据,EXISTS比连接查询效率要好
      

  4.   


    ----测试语句时间set statistics time on 
    select * from sysprotects
    set statistics time off
      

  5.   

    如果你只想显示card表的数据
    不需要采用链接的形式
    推荐使用第二种 exists
      

  6.   

    由于执行exists时需要检查的项目比较少,能比别的如in和其他的.
      

  7.   


    显示 执行SQL语句 select * from sysprotects  所花的时间