SQL中and和or优先级

优先规则

求值顺序  
1 算术运算
2 连字操作
3 比较操作
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 NOT 逻辑条件
7 AND 逻辑条件
8 OR 逻辑条件

使用圆括号改变优先规则

如果不带括号,where条件中and和or共存时,会以or来分割

在emp表中查询,薪水大于1500,部门号为10或者奖金大于0的记录:

select * from emp where sal >1500 and deptno = 10 or comm > 0;

上面一句结果会和预期不符,因为or优先级比and低,所以where后的条件分为sal >1500 and deptno = 10(条件一)或者comm > 0(条件二)。

遇到这种情况需要加括号:

select * from emp where sal >1500 and (deptno = 10 or comm > 0);

参考资料:
http://blog.163.com/xiaosanshaoli@126/blog/static/2979216320093294392425/