首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >蜂巢中的“和/”或“操作顺序”

蜂巢中的“和/”或“操作顺序”
EN

Stack Overflow用户
提问于 2018-07-25 19:38:57
回答 2查看 2.6K关注 0票数 1

我正在使用Hadoop2.7.3。当您运行hiveql命令并在其中包含“and”和“或”的where子句时,它是如何分配条件的?

例如,假设我有以下查询:

代码语言:javascript
复制
... where A and B or C and D.

它是否返回下列内容之一:

代码语言:javascript
复制
A or B or C or D
((A and B) or C) and D
(A and B and D) or C
A and (B or C) and D

我知道我可以使用括号来指定上面使用的确切内容,但是默认情况下它会做什么呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-25 19:52:55

@GordonLinoff的答案是正确的。您可以通过使用以下查询构造真值表来验证这一点:

代码语言:javascript
复制
SELECT *, A and B or C and D AS result
FROM
  (SELECT TRUE AS a
   UNION ALL SELECT FALSE AS a) A
CROSS JOIN
  (SELECT TRUE AS b
   UNION ALL SELECT FALSE AS b) B
CROSS JOIN
  (SELECT TRUE AS c
   UNION ALL SELECT FALSE AS c) C
CROSS JOIN
  (SELECT TRUE AS d
   UNION ALL SELECT FALSE AS d) D

哪个产出:

代码语言:javascript
复制
+-----+-----+-----+-----+-------+
|  a.a|  b.b|  c.c|  d.d| result|
+-----+-----+-----+-----+-------+
| true| true| true|false|   true|
| true| true| true| true|   true|
| true| true|false|false|   true|
| true| true|false| true|   true|
|false| true| true|false|  false|
|false| true| true| true|   true|
|false| true|false|false|  false|
|false| true|false| true|  false|
| true|false| true|false|  false|
| true|false| true| true|   true|
| true|false|false|false|  false|
| true|false|false| true|  false|
|false|false| true|false|  false|
|false|false| true| true|   true|
|false|false|false|false|  false|
|false|false|false| true|  false|
+-----+-----+-----+-----+-------+

因此,我们可以从经验上得出这样的结论,即这确实是(A and B) or (C and D)的结果。

票数 2
EN

Stack Overflow用户

发布于 2018-07-25 19:42:24

这是操作的优先级。ANDOR绑定得更紧密,因此:

代码语言:javascript
复制
A and B or C and D

解释为:

代码语言:javascript
复制
(A and B) or (C and D)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51526491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档