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

本文详细解析了SQL中in/not in与exists/not exists的区别及应用场景。in/not in用于判断单个属性是否匹配指定值,而exists/not exists则用于处理两个表之间的交集和差集问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值