Grouping AND and OR conditionals in PostgreSQL

て烟熏妆下的殇ゞ 提交于 2020-01-14 07:32:18

问题


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 ?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10331613/grouping-and-and-or-conditionals-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!