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

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%';
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章