MS SQL 中 exists 和 not exists子查询的用法一直都没弄懂 谁能说明一下 他的用法吗 ?
谢谢!
我说下我一直怎么理解的!
比如:
SELECT title FROM titles WHERE NOT EXISTS (SELECT title_id FROM sales WHERE title_id = titles.title_id)我的理解就是,如果NOT EXISTS后面的“(SELECT title_id FROM sales WHERE title_id = titles.title_id)”有结果集,为true,那么“NOT EXISTS”后,“SELECT title FROM titles ”就不该执行;这样理解显然不对!我对“EXISTS”用法的理解就是,它是一个判断,只返回真或假,
当子查询里只要有返回行就为真,没有就为假!请各位高手抽空给我详细的讲一下,最好有例子,谢谢 

解决方案 »

  1.   

    exists      存在
    not exists  不存在
      

  2.   

    create table test
    (
    id int
    )
    insert test
    select 1 union select 2 union select 3
    select * from test where not exists(select * from test where id=1)select * from test where exists(select * from test where id=1)
    drop table test
    --------------------
    id          
    ----------- (所影响的行数为 0 行)id          
    ----------- 
    1
    2
    3(所影响的行数为 3 行)
      

  3.   

    select * from a where exists (select * from b where id=a.id)
    说明:a.id=b.id的所有a表数据.反之not exists ,不包含a.id=b.id所有a表数据
      

  4.   

    roy_88 的解释很清楚,谢谢
    同时谢谢大家的帮助,我看看怎么给分哦