在PostgreSQL中对AND和OR条件进行分组

时间:2022-05-15 22:42:31

I always use brackets in sql queries. But I have example:

我总是在SQL查询中使用括号。但我有一个例子:

DELETE FROM prog 
WHERE prog_start >= $1 AND prog_start < $2
   OR prog_end > $1 AND prog_end <= $2

Is it equal to :

它等于:

DELETE FROM prog
WHERE ( prog_start >= $1 AND prog_start < $2 )
   OR ( prog_end > $1 AND prog_end <= $2 ) 

or not ?

或不 ?

2 个解决方案

#1


9  

In SQL the AND operator takes "precedence" over OR operator. PostgreSQL adheres to the spec here. You can the exact precedence in PostgreSQL in the docs Lexical Structure: Operator Precedence.

在SQL中,AND运算符优先于OR运算符。 PostgreSQL遵循这里的规范。您可以在文档词汇结构:运算符优先级中使用PostgreSQL中的确切优先级。

So in your case, the result will be the same. However, it's much easier, and cleaner to simply use the parentheses.

所以在你的情况下,结果将是相同的。但是,简单地使用括号更简单,更简洁。

#2


0  

It goes as per the Operator Precendence http://www.postgresql.org/docs/6.5/static/operators.htm#AEN1615.

它按照运营商优先权http://www.postgresql.org/docs/6.5/static/operators.htm#AEN1615进行。

To form a complex condition it's always better to parenthesis your conditions.

要形成一个复杂的条件,最好将括号括起来。

#1


9  

In SQL the AND operator takes "precedence" over OR operator. PostgreSQL adheres to the spec here. You can the exact precedence in PostgreSQL in the docs Lexical Structure: Operator Precedence.

在SQL中,AND运算符优先于OR运算符。 PostgreSQL遵循这里的规范。您可以在文档词汇结构:运算符优先级中使用PostgreSQL中的确切优先级。

So in your case, the result will be the same. However, it's much easier, and cleaner to simply use the parentheses.

所以在你的情况下,结果将是相同的。但是,简单地使用括号更简单,更简洁。

#2


0  

It goes as per the Operator Precendence http://www.postgresql.org/docs/6.5/static/operators.htm#AEN1615.

它按照运营商优先权http://www.postgresql.org/docs/6.5/static/operators.htm#AEN1615进行。

To form a complex condition it's always better to parenthesis your conditions.

要形成一个复杂的条件,最好将括号括起来。

相关文章