简体   繁体   中英

Grouping AND and OR conditionals in PostgreSQL

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

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 ?

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 .

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM