presto、sql用not exists选择表A中的所有id但剔除掉表B中的id

34 篇文章 5 订阅
SELECT student_id,num
FROM
 table_A AS a----别名,注意哦 这里没有括号
 WHERE
 channel = 3

AND NOT EXISTS -----别只会做加法,presto、SQL做减法查询,凡是另一个表出现的都不要都剔除
( SELECT 1 FROM table_B AS b -----别名,as可以省略
WHERE
 a.student_id =  b.student_id)
order by  student_id desc

额外说一下,sql和presto、hive中,not exists 与not in的一点区别
使用not exists,将条件下推到里面,就不会出现子查询:


```sql
select * 
from test  t1 
where info like '%test%' 
and not exits (select 1 from test_back t2 where t2.id = t1.id)



使用 not in就会造成极大的性能损耗,例如:

select * 
from test 
where id not in (select id from test_back) 
and info like '%test%';
----not in 参考代码
select sno,sname 
from student 
where sno not in
  (select sno 
   from SC 
   where cno in
     (select cno 
      from course
      where tno in(
         select tno from teacher
         where tname='悟空')
)
)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值