码迷,mamicode.com
首页 > 其他好文 > 详细

and和or的优先级

时间:2017-05-12 22:02:17      阅读:448      评论:0      收藏:0      [点我收藏+]

标签:查看   code   优先   name   cond   业务   sel   括号   多个   


and级别高于or

例子:

select * from test where condition1 or condition2 and condition3

其执行效果与下面的sql等价:

select * from test where condition1 or (condition2 and condition3);

多个and 和 or 混用也是这个道理,相当于把 and 看成乘号(*), 把 or 看成加号(+),这样sql的执行顺序就一目了然了。

最后还是建议大家严格按照业务逻辑写sql,必要的时候毫不吝啬使用括号,就像上面的情况,如果逻辑是:condition1 且 condition2 或者 condition3,那么请写成:

select * from test where (condition1 and condition2) or condition3;

这样既不会出错,也方便以后查看代码。何乐而不为呢。

and和or的优先级

标签:查看   code   优先   name   cond   业务   sel   括号   多个   

原文地址:http://www.cnblogs.com/huang-yu/p/6847158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!