SQL中NOT EXISTS的使用


  本文标签:SQL中NOT EXISTS

  在SQL中NOT EXISTS的作用是什么呢?下文就将带您寻找答案,看看SQL中NOT EXISTS究竟如何使用,供您参考  。

  SQL中NOT EXISTS 的作用与 EXISTS 正相反  。如果子查询没有返回行,则满足SQL中NOT EXISTS 中的 WHERE 子句  。本示例查找不出版商业书籍的出版商的名称:

  1. USE pubs  
  2. G  
  3. OSELECT pub_name  
  4. FROM publishers  
  5. WHERE NOT EXISTS     
  6. (SELECT *    
  7.  FROM titles     
  8. WHERE pub_id = publishers.pub_id     
  9. AND type = \business\)  
  10. ORDER BY pub_name  
  11. GO 

  下面是结果集:

  1. pub_name                                
  2. ----------------------------------------   
  3. Binnet & Hardley                           
  4. Five Lakes Publishing                     
  5. GGG&G                                      
  6. Lucerne Publishing                         
  7. Ramona Publishers                          
  8. Scootney Books                             
  9. (6 row(s) affected)