您的位置:首页 > 博客中心 > 数据库 >

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

时间:2022-03-14 02:01

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;








热门排行

今日推荐

热门手游