PostgreSQL的括号规则,是否有汇总指南? [英] The parentheses rules of PostgreSQL, is there a summarized guide?

查看:395
本文介绍了PostgreSQL的括号规则,是否有汇总指南?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数学和许多编程语言(我也认为是标准SQL )中,括号更改优先级(将要评估的零件分组)或提高可读性(对于人眼)。

等效示例:

In Mathematics and many programming languages (and I think standard SQL as well), parentheses change precedence (grouping parts to be evaluated first) or to enhance readability (for human eyes).
Equivalent Examples:

SELECT array[1,2] @> array[1]
SELECT (array[1,2]) @> array[1]

SELECT array[1,2] @> (array[1])
SELECT ((array[1,2]) @> (array[1]))

但是 SELECT 1 = ANY数组[1,2] 是语法错误(!),而 SELECT 1 = ANY(array [1,2])有效。为什么?

But SELECT 1 = ANY array[1,2] is a syntax error (!), and SELECT 1 = ANY (array[1,2]) is valid. Why?

好,因为手册中是这样说的 。但是人类记住所有异常的逻辑是什么?

是否有相关指南?

OK, because "the manual says so". But what the logic for humans to remember all exceptions?
Is there a guide about it?

我没有了解为什么(表达式)在某些情况下与表达式相同,而在其他情况下却不相同。

I do not understand why (expression) is the same as expression in some cases, but not in other cases.

PS1:括号也用作值列表定界符,如表达式IN(值[,.. 。])。但是数组不是值列表,并且当(数组表达式)数组表达式。

另外,我以数组为例,但是这个问题/问题不仅与数组有关。

PS1: parentheses are also used as value-list delimiters, as in expression IN (value [, ...]). But an array is not a value-list, and there does not seem to be a general rule in PostgreSQL when (array expression) is not the same as array expression.
Also, I used array as example, but this problem/question is not only about arrays.

推荐答案

是否有汇总指南? ,嗯...答案是,所以:动手!

"Is there a summarized guide?", well... The answer is no, so: hands-on! This answer is a Wiki, let's write.

让我们


  • F ()是一个常用函数。 (例如 ROUND

  • L ()是类似于函数的运算符(例如 ANY

  • f 是类似于运算符的函数(例如 current_date

  • Op 运算符

  • Op1 Op2 是不同的运算符

  • A,B,C值或表达式

  • S一个表达式列表,例如((A,B,C )

  • F() a an usual function. (ex. ROUND)
  • L() a function-like operator (ex. ANY)
  • f a operator-like function (ex. current_date)
  • Op an operator
  • Op1, Op2 are distinct operators
  • A, B, C values or expressions
  • S a expression-list, as "(A,B,C)"

使用这些元素的规则的形式为

The rules, using these elements, are in the form


  • 规则:笔记。

  • rule: notes.

Op Op1 Op2 是数学运算符(例如 + - * )和 F ()是数学函数(例如 ROUND())。

When Op, Op1, Op2 are mathematical operators (ex. +, -. *), and F() is a mathematical function (ex. ROUND()).

标量表达式的规则数组表达式:


  • A Op B =(A Op B):括号ses是可选的。

  • A Op1 B Op2 C :需要检查优先级

  • (A Op1 B) Op2 C :强制执行第一个(A Op1 B)。

  • A Op1 (B Op2 C):强制执行第一个(B Op2 C)。

  • F (A)=( F (A))= F ((A))=( F (( A))):括号是可选的。

  • S =(S):外部括号是可选的。

  • f =( f :括号是可选的。

  • A Op B = (A Op B): the parentheses is optional.
  • A Op1 B Op2 C: need to check precedence.
  • (A Op1 B) Op2 C: enforce "first (A Op1 B)".
  • A Op1 (B Op2 C): enforce "first (B Op2 C)".
  • F(A) = (F(A)) = F((A)) = (F((A))): the parentheses are optional.
  • S = (S): the external parentheses are optional.
  • f=(f): the parentheses are optional.

运算符的规则为 ALL ANY ROW SOME

Rules for operators as ALL, ANY, ROW, SOME, etc.


  • L (A)= L ((A)):括号在参数中是可选的。

  • L (A)):语法错误。

  • L(A) = L((A)): the parentheses is optional in the argument.
  • (L(A)): SYNTAX ERROR.

...更多规则?请在这里帮助编辑。

...More rules? Please help editing here.

这篇关于PostgreSQL的括号规则,是否有汇总指南?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆