hive sql 條件放在on和where區(qū)別比較

原理:

關聯(lián)時會先創(chuàng)建臨時表t1和t2,where后面的條件會先過濾t1诉稍、t2臨時表后在關聯(lián),on后面的條件會先關聯(lián)t1最疆、t2后在過濾杯巨。

結論:

條件在on后面,主表數(shù)據(jù)量不變努酸,副表只顯示符合條件的服爷。
條件在where后面,先過濾條件在關聯(lián)获诈。
測試時直接替換dmp庫就可以執(zhí)行

-- 條件放在on和where后面的區(qū)別:
use tmp;
drop table tmp.yl_test_1;
drop table tmp.yl_test_2;
create table tmp.yl_test_1(id int,name string,birthday string);
create table tmp.yl_test_2(id int,age int,birthday string);
insert into tmp.yl_test_1 values(1,'aa','2023-12-01');
insert into tmp.yl_test_1 values(2,'bb','2023-12-12');
insert into tmp.yl_test_1 values(3,'cc','2023-12-30');
insert into tmp.yl_test_2 values(1,40,'2023-12-04');
insert into tmp.yl_test_2 values(2,50,'2023-12-10');
insert into tmp.yl_test_2 values(2,50,'2023-12-20');

select * from tmp.yl_test_1 t1;
id  name    birthday
1   aa  2023-12-01
2   bb  2023-12-12
3   cc  2023-12-30

select * from tmp.yl_test_2 t1;
id  age birthday
1   40  2023-12-04
2   50  2023-12-10
2   50  2023-12-20

select * from tmp.yl_test_1 t1 
left join 
tmp.yl_test_2 t2 on t1.id = t2.id;
id  name    birthday    id2 age birthday2
2   bb  2023-12-12  2   50  2023-12-10
2   bb  2023-12-12  2   50  2023-12-20
1   aa  2023-12-01  1   40  2023-12-04
3   cc  2023-12-30  \N  \N  \N

select * from tmp.yl_test_1 t1 
left join 
tmp.yl_test_2 t2 on t1.id = t2.id and t1.id = 3;
id  name    birthday    id2 age birthday2
1   aa  2023-12-01  \N  \N  \N
2   bb  2023-12-12  \N  \N  \N
3   cc  2023-12-30  \N  \N  \N

select * from tmp.yl_test_1 t1 
left join 
tmp.yl_test_2 t2 on t1.id = t2.id where t1.id = 3;
id  name    birthday    id2 age birthday2
3   cc  2023-12-30  \N  \N  \N

select * from tmp.yl_test_1 t1 
left join 
tmp.yl_test_2 t2 on t1.id = t2.id and t2.id = 3;
id  name    birthday    id2 age birthday2
1   aa  2023-12-01  \N  \N  \N
2   bb  2023-12-12  \N  \N  \N
3   cc  2023-12-30  \N  \N  \N

select * from tmp.yl_test_1 t1 
left join 
tmp.yl_test_2 t2 on t1.id = t2.id where t2.id = 3;
id  name    birthday    id2 age birthday2

select * from tmp.yl_test_1 t1 
left join 
tmp.yl_test_2 t2 on t1.id = t2.id and t1.birthday > t2.birthday;
id  name    birthday    id2 age birthday2
3   cc  2023-12-30  \N  \N  \N
2   bb  2023-12-12  2   50  2023-12-10
1   aa  2023-12-01  \N  \N  \N

select * from tmp.yl_test_1 t1 
left join 
tmp.yl_test_2 t2 on t1.id = t2.id where t1.birthday > t2.birthday;
id  name    birthday    id2 age birthday2
2   bb  2023-12-12  2   50  2023-12-10

select * from tmp.yl_test_1 t1 
left join 
tmp.yl_test_2 t2 on t1.id = t2.id and t1.birthday < t2.birthday;
id  name    birthday    id2 age birthday2
3   cc  2023-12-30  \N  \N  \N
2   bb  2023-12-12  2   50  2023-12-20
1   aa  2023-12-01  1   40  2023-12-04

select * from tmp.yl_test_1 t1 
left join 
tmp.yl_test_2 t2 on t1.id = t2.id where t1.birthday < t2.birthday;
id  name    birthday    id2 age birthday2
2   bb  2023-12-12  2   50  2023-12-20
1   aa  2023-12-01  1   40  2023-12-04
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末仍源,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子舔涎,更是在濱河造成了極大的恐慌笼踩,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件亡嫌,死亡現(xiàn)場離奇詭異嚎于,居然都是意外死亡掘而,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門匾旭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來镣屹,“玉大人,你說我怎么就攤上這事价涝∨冢” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵色瘩,是天一觀的道長伪窖。 經(jīng)常有香客問我,道長居兆,這世上最難降的妖魔是什么覆山? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮泥栖,結果婚禮上簇宽,老公的妹妹穿的比我還像新娘。我一直安慰自己吧享,他們只是感情好魏割,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著钢颂,像睡著了一般钞它。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上殊鞭,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天遭垛,我揣著相機與錄音,去河邊找鬼操灿。 笑死锯仪,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的趾盐。 我是一名探鬼主播卵酪,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼谤碳!你這毒婦竟也來了溃卡?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤蜒简,失蹤者是張志新(化名)和其女友劉穎瘸羡,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體搓茬,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡犹赖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年队他,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片峻村。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡麸折,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出粘昨,到底是詐尸還是另有隱情垢啼,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布张肾,位于F島的核電站芭析,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏吞瞪。R本人自食惡果不足惜馁启,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望芍秆。 院中可真熱鬧惯疙,春花似錦、人聲如沸妖啥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽迹栓。三九已至,卻和暖如春俭缓,著一層夾襖步出監(jiān)牢的瞬間克伊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工华坦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留愿吹,地道東北人。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓惜姐,卻偏偏與公主長得像犁跪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子歹袁,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容