oracle中逻辑运算符(not,and,or)及其优先级

逻辑运算符

意义

and

双值运算符,如果左右两个条件都为真,则得到的值就为真

or

双值运算符,只要左右两个条件有一个为真,则得到的值就为真

not

单指运算符,如果原条件为真,则得到真,如果元条件为假,反之如果原条件为假,则结果为真

select * from emp where sal > 2000 and job = 'salesman';

 

寻找那些工资高于2000的且职位为销售的职员。

 

select * from emp where job = 'clerk' or deptno = 20;

 

寻找那些工作为CLERK或者所在部门标号为20的职员的列表

 

select * from emp where not (sal > 3000 or sal < 1500);

 

寻找那些工资既不大于3000也不小于1500,也即在1500到3000范围的员工,

相当于:select * from emp where sal between 1500 and 3000;

 oracle中所有运算符的优先级如下:

运算符

级别

算术运算符(即‘+’,‘-’,‘*’,‘/’)

1

连接运算符(即‘||’)

2

比较运算符(即‘>’,‘>=’,‘<’,‘<=’,‘<>’)

3

Is [not] null,[not] like,[not] in

4

[not] between-and

5

not

6

and

7

or

8

通常使用()可以改变运算符的优先级。

 

需要注意的是and的优先级要优于or,也就是说下面的语句

select * from emp where sal < 1500 or sal >= 2000 and job = 'analyst';

 

等价于:select * from emp where sal < 1500 or (sal >= 2000 and job = 'analyst');

 

而不是你所预期的

select * from emp where (sal < 1500 or sal >= 2000) and job = 'analyst';

 

一般即使我们要表达第一个语句所要表达的意思,为了避免误解,都不采取第一种写法,而是用括号来表明我们要先算后面的部分。

猜你喜欢

转载自blog.csdn.net/weixin_41561862/article/details/108828038