zoukankan      html  css  js  c++  java
  • Oracle 语句

    一,select

    select 用于从数据看查询数据。语法:

    select field1,filed2,.. .
    from tablename
    [where condition]

    举例:

    -- 查询所有员工的名字和雇员号
    select e.ename,e.empno
    from emp e;

    * 通配符表示查询所有字段。如果要查特定字段时,不要使用*,影响查询效率

    select * from emp;

    distinct(修饰多字段时,多个字段的值都不一样才保留

    -- 查询公司的工种
    select distinct e.job
    from emp e;

    where字句

    where 表示查询的条件。

    [1] =,!= ,<>,<,>,<=,>= 关系运算符

    <> 表示不等于

    -- 把部分10的雇员查询出来
    select * 
    from emp 
    where deptno = 10;
    
    -- 把名称为smith的雇员
    select e.*
    from emp e
    where e.ename = 'SMITH';
    
    -- 查询底薪大于等于1000的员工
    select e.*
    from emp e
    where e.sal >= 1000;
    
    select e.*
    from emp e
    where e.sal <> 800

    any/some/all (list)

    any/some(list) 满足list列表中的任意一个条件

    all(list) 满足list列表的中所有条件

    -- 查询薪资大于1000或者薪资大于800的雇员
    select e.*
    from emp e
    where e.sal > some(1000,800);
    
    -- 查询薪资大于1000
    select e.*
    from emp e
    where e.sal > all(1000,800);

    [2] null

    null sql中表示的是不确定 => 可以认为没有值

    -- 查询没有津贴的雇员
    select e.*
    from emp e
    where e.comm is null
    
    select e.*
    from emp e
    where e.comm is not null

    [3] between x and y

    表示一个值位于[x,y]区间,x/y 一般都是数字

    -- 查询薪资在1000-5000之间的雇员
    select e.*
    from emp e
    where e.sal between 1000 and 5000
    
    -- 查询薪资在(3000,5000]之间的雇员
    select e.*
    from emp e
    where e.sal between 3000.01 and 5000

    [4] in/not in list

    表示字段值是否在list列表中

    -- 查询部分号是10和20的员工
    select e.*
    from emp e
    where e.deptno in(10,20);
    
    select e.*
    from emp e
    where e.deptno not in(10,20);
    
    -- 查询薪资是1000,2000,5000的员工
    select e.*
    from emp e
    where e.sal in (1000,2000,5000);

    [5] 模糊查询

    like 关键字用于模糊查询,其中

    %:表示任意字符出现多次(0),

    _:表示任意字符出现1次。


    escape(‘x’) 表示指定转义字符为x,一般指定为

    -- 查询名字是c开头的雇员
    
    select e.*
    from emp e
    where e.ename like 'c%';
    
    -- 查询名字中第二个字母是M的雇员
    select e.*
    from emp e
    where e.ename like '_M%'
    
    -- 查询名字中含有M的雇员
    select e.*
    from emp e
    where e.ename like '%M%';
    
    -- 查询名字中含有%的雇员
    select e.*
    from emp e
    where e.ename like '%\%%' escape('');

    二,复杂查询

    where 后面的条件可以跟多个通过and 或者 or 连接

    and:且、并且

    or: 或、或者

    -- 查询部门10且薪资大于等2000的雇员
    select e.*
    from emp e
    where e.deptno = 10 and e.sal >= 2000;
    
    -- 查询名字中含M且薪资大于1000的雇员
    select e.*
    from emp e
    where e.ename like '%M%' and e.sal > 1000
    
    -- 查询部门在10或20的雇员
    select e.*
    from emp e
    where e.deptno = 10 or e.deptno = 20

    思考:查询条件的顺序对查询速度是否有影响?

    分析:

    and 表示且,条件越多,检索的数据量越来越少

    or 表示或,条件越多,检索的数据量越来越多

    where 条件的执行顺序从后向前

     

    -- 优化后的sql
    select e.*
    from emp e
    where e.sal>=2000 and e.deptno = 10

     

    AND: 把检索结果较少的条件放到后面(先执行)

    三,计算字段

     

     我们经常需要把数据库中检索出来的信息进行再加工,允许的操作+-*/。通过四个运算得到新的字段(计算字段)

     

    计算字段在数据表中不存在

    -- 查询出每个雇员的月薪(收入)
    select e.ename,e.sal+e.comm as "收入",e.deptno
    from emp e

    运算结果:

     注意:很多记录中的commnull,表示不确定的值,经常四则运算后的值也不确定。


    当遇到字段时null,可以通过nvl函数把null转化便于运算的类型

    -- nvl函数优化
    select e.ename,e.sal+nvl(e.comm,0) "收入",e.deptno
    from emp e

    四,函数

    函数一般是在数据上执行的,它给数据的转换和处理提供了方便。只是将取出的数据进行处理,不会改变数据库中的值。

    函数根据处理的数据分为单行函数和聚合函数(组函数)

    组函数又被称作聚合函数,用于对多行数据进行操作,并返回一个单一的结果,组函数仅可用于选择列表或查询的having子句

    单行函数对单个数值进行操作,并返回一个值

     五,字符相关

    常用计算字符举例

    -- dual用于测试
    select * from dual;
    
    -- 1.字符串连接
    select concat('aa','12') from dual;
    select 'aa'||'12' from dual;
    
    -- 2.首字母大写
    select initcap('abc') from dual;
    --- 把大写转化小写
    select lower('ABc') from dual;
    select upper('abc') from dual;
    
    -- 把所有员工的姓名小写输出
    select lower(e.ename),e.empno
    from emp e
    
    -- 3.填充字符lpad/rpad
    select lpad('sxt',5,'*') from dual;
    select rpad('sxt',5,'*') from dual;
    
    -- 4.去掉空白字符
    select '  kallen' from dual;
    select ltrim('  kallen',' ') from dual;
    select rtrim('  kallen  ',' ') from dual;
    -- trim 删除左右两边的字符
    select trim('a' from 'abc') from dual;
    
    -- 5.求子串 substr(str,loc,len)-->loc从1开始
    select substr('abcd',2,2) from dual;
    
    -- 6.查找字符串
    /*
    如果找到返回>=1的索引;如果没找到返回0
    */
    select instr('abcd','b') from dual;
    
    -- 7.求长度
    select length('abcd') from dual;

     

  • 相关阅读:
    几种委托的解释
    Python中的编码风格
    Python的循环
    Python中操作文件
    Python的random模块、string模块、time模块、os模块
    Python中的函数
    Python的数据类型
    使用iview Form 的resetFields()在f12下报错
    滚动条的滚动距离
    编程学习之资源
  • 原文地址:https://www.cnblogs.com/zhangxiong-tianxiadiyi/p/10858496.html
Copyright © 2011-2022 走看看