码迷,mamicode.com
首页 > 数据库 > 详细

sql中in/not in 和exists/not exists的用法区别

时间:2014-11-05 17:25:36      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:sp   bs   as   sql   type   nbsp   c   class   rom   

1;首先来说in/not in的用法

      in/not in是确定单个属性的值是否和给定的值或子查询的值相匹配;

     

     select * from Student s where s.id in(1,2,3);

<pre name="code" class="sql">     select * from Student s where s.name in( select distinct name from Project)

2;现在来说exists/not exists的用法
   exists/not exists是解决两张表的交集和差集

   select * from proj_basic_info p  where   not exists (select * from proj_basic_info q where   p.id=q.id and q.proj_type=1 and q.qy_source='SUAEE')

  <pre name="code" class="sql">   select * from proj_basic_info p  where  exists (select * from proj_basic_info q where   p.id=q.id and q.proj_type=1 and q.qy_source='SUAEE')

总结:如果是判断单个属性是是否匹配时,就选in/not in;如果是判断多个属性时,即可以看做集合时,就选用exitst /not exists;








sql中in/not in 和exists/not exists的用法区别

标签:sp   bs   as   sql   type   nbsp   c   class   rom   

原文地址:http://blog.csdn.net/u011079727/article/details/40823107

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!