Oracle 运算符及其优先级

来源:互联网 发布:html网站源码下载 编辑:程序博客网 时间:2024/05/21 17:40

Oracle中所有运算符的优先级

运算符

级别

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

1

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

2

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

3

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

4

[not] between-and

5

not

6

and

or

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

Oracle的逻辑运算符也是用在SQL语句中必不可少的因素,一共有三个

逻辑运算符

意义

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;

需要注意的是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’;

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

转载自:

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

http://hi.baidu.com/jskhf/blog/item/cba5801281c1cd055baf534c.html

原创粉丝点击