昨天我在嘗試寫一個很簡單的HiveQL:語句如下
select <!subdate(date,0)!>,a.ver,a.vid,a.wid,a.type,count(*) from (select stat_date,ver,get_json_object(json,"$.vid") as vid,get_json_object(json,"$.wid") as wid,get_json_object(json,"$.type") as type from iphonevv_<!subdate(date,0)!> where stat_date=<!subdate(date,0)!> and ver >= '3.5.0' and get_json_object(json,"$.type")=4 or get_json_object(json,"$.type")=5 or get_json_object(json,"$.type")=6 )a group by a.stat_date,a.ver,a.vid,a.wid,a.type
我的想法是:stat_date=<!subdate(date,0)!> and ver >= '3.5.0',後面的都是or就行,但是結果並不是這樣,出現了很多ver不是大於等於3.5.0的,我糾結了很久,都不知道是哪兒出了問題,為什麼ver >= '3.5.0' 不起作用呢?今天查了下才發現,原來是where後面的and和or的問題導致。
結果後來我修改為:
select <!subdate(date,0)!>,a.ver,a.vid,a.wid,a.type,count(*) from (select stat_date,ver,get_json_object(json,"$.vid") as vid,get_json_object(json,"$.wid") as wid,get_json_object(json,"$.type") as type from iphonevv_<!subdate(date,0)!> where stat_date=<!subdate(date,0)!> and ver >= '3.5.0' and (get_json_object(json,"$.type")=4 or get_json_object(json,"$.type")=5 or get_json_object(json,"$.type")=6 ))a group by a.stat_date,a.ver,a.vid,a.wid,a.type
將or的條件放在()之內,問題就解決了。
where 後面如果有and,or的條件,則or自動會把左右的查詢條件分開,即先執行and,再執行or。原因就是:and的執行優先順序最高!
關係型運算子優先順序高到低為:not and or
問題的解決辦法是:
用()來改變執行順序!!!!