HiveQL中where后面的and 和or 处理

来源:互联网 发布:淘宝怎么购买二手店铺 编辑:程序博客网 时间:2024/05/21 06:13

昨天我在尝试写一个很简单的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

问题的解决办法是:

用()来改变执行顺序!!!!


原创粉丝点击