sql中in/not in 和exists/not exists的使用方法差别

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

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

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

     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')

   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。





原文地址:https://www.cnblogs.com/liguangsunls/p/6793069.html