数据库查询-关于exists的使用

索引使用

1)select * from A where id in(select id from B)   -->效率低,用到了A表上id列的索引

2)select * from A where exists(select id from B where id=A.id) -->效率高,用到了B表上id列的索引

3)select * from B where id in(select id from A)   -->效率高,用到了B表上id列的索引

4)select * from B where exists(select id from A where id=B.id) -->效率低,用到了A表上id列的索引

 

select a1 from a where a1 not EXISTS(select 1 FROM B WHERE a.a1=b.a1)

select a1 from a where a1 not in(select a1 FROM B WHERE a.a1=b.a1)

 

select * from t
where exists(select 1 from t1 where t.no=t1.no and t1.name='211')
and exists(select 1 from t2 where t.no=t2.no and t2.name='211')

与下面的查询结果一样
select no from t1 where t1.name='211'
interact
select no from t2 where t2.name='211'

union 并集操作,不包括重复行
union all 并集操作,包括重复行
intersect 交集,不包括重复行
minus 差操作,不包括重复行

转载于:https://www.cnblogs.com/morningdaylight/p/9968906.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值