zoukankan      html  css  js  c++  java
  • oracle中逻辑运算符(not,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;

    结合到前面所列出的各类运算符,在这里汇总一下oracle中所有运算符的优先级

    运算符

    级别

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

    1

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

    2

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

    3

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

    4

    [not] between-and

    5

    not

    6

    and

     

    or

     

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

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

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

  • 相关阅读:
    手动在本机仓库中放置jar包以解决Maven中央仓库不存在该资源的问题
    同一套代码部署到两台机器上,只有一台机器上的页面中文乱码
    Nginx与httpd共存
    [Z3001] connection to database 'zabbix' failed: [2003] Can't connect to MySQL server on 'x.x.x.x' (13)
    Excel中时间戳转换公式及原理
    springcloud服务注册和发现
    spingboot和springcloud简记
    postgres use
    访问者模式
    uml类图详解
  • 原文地址:https://www.cnblogs.com/dongxiaoguang/p/2966647.html
Copyright © 2011-2022 走看看