HiveQL中where後面的and 和or 處理

來源:互聯網
上載者:User
昨天我在嘗試寫一個很簡單的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

問題的解決辦法是:

用()來改變執行順序!!!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.