java培训
搜索分类

条件查询之and和or的优先级

赋能网 2023-06-11 117

and和or的优先级

  • 查询薪水大于1800,并且部门代码为20或30的员工(错误的写法)

select * from emp where sal > 1800 and deptno = 20 or deptno = 30;

以上输出不是预期结果,薪水小于1800的数据也被查询上来了,原因是表达式的优先级导致的,首先过滤sal > 1800 and deptno = 20,然后再将deptno = 30员工合并过来,所以是不对的

  • 查询薪水大于1800,并且部门代码为20或30的(正确的写法)

select * from emp where sal > 1800 and (deptno = 20 or deptno = 30);

条件查询之and和or的优先级

关于运算符的问题:不用记,没有把握尽量采用括号

发表评论
0评