oracle 之 基本操作

 

oracle执行语句的执行顺序:

1. where 子句,筛选行,后面只能出现行信息

  select * from table t where t.xxx = '';

   该语句的执行顺序:  1.from ...

            2.where ...

            3. select *

2.group by 子句,

  select * from table x group by ...

  该语句的执行顺序:  1. from ...

            2.group by ...

            3. select *

  select deptno, avg(sal) from emp group by deptno;

3. having 子句,过滤组, 后面只能出现组信息 

  select * from table x  where ...group by ... having ...

  该语句的执行顺序:   1. from ...

            2. where ...

            3. group by ...

            4. having ...

            5. select *

  --只统计工资大于2000,并且工资大于2000的员工数量大于等于2以上的部门

  select deptno,  count(*)  from emp where sal > 2000 group by deptno having count(*) >= 2;

4. oracle分页

  select * from (select um, name, rownum rw from emp ) where rw >= 5 and rw < 10;

  --对结果集进行排序后再分页

  select ename, sal, deptno, r1, r2 from (

    select ename, sal, deptno, r1, rownum r2 from (

      select ename, sal, deptno, rownum r1 from emp order by sal desc

      )

  )

  where r2 > (n-1)*3

    and r2 <= n*3;

 

5. 等值连接

  --查询出每一个有员工存在部门的信息和部门的人数

  分析: --查询数据:部门信息,对应的部门的人数

      --数据来源:dept, emp, 经过计算

      --连接条件:dept.deptno = 人数对应的.deptno

  --查询出有员工的部门的员工人数

  select count(*),  deptno from emp group by deptno ;    

  sql:

    select * from dept d, (select count(*),  deptno from emp group by deptno) c 

    where d.deptno = c.deptno ;   

6. 非等值连接

  !=.   >.  <.  <>.  between  and 

  

 

一、NUll值处理

  null比较特殊,需要单独处理, 当null进行算数运算是,其运算结果都为null;

  is null; is not null; not ... is null;

  nulls first, nulls last

  a. order by 包含null进行排序

  select * from emp order by comm desc nulls last;

  

二、Nvl函数(字段,默认值)

  奖金为空的数据:

  select * from emp where nvl(comm, 0) <= 0;

  

三、拼接 符号 || 

  || 遇到null时,null自动转换为空字符串

  select ename, ename || comm from emp;

四. 模糊查询 līke

  %   _ 

  当查询条件比较模糊时;

  例: --名称中包含有A的工作

    select * from emp where job like '%A%';

    --查询名称当中第二个字母有A的名字

    select * from emp where ename like '_A%';  // _表示一个字符

    --名称当中倒数第二个字母为A的名字

    select * from emp where ename like '%A_'; 

    --查询员工名称中包含%的信息 

    select * from emp where ename like '%a%%' escape('a');  //a后面的字符表示为查询参数,只标示后面一位的字符

函数:

1.常用单行函数

1.1 字符函数

  concat(x, y)       连接字符串x 和 y

  instr(x, str, start, n)     在x中查找str, 可以指定从start开始,也可以指定从第n次开始

  length(x)          返回x的长度

  lower(x)         x转为小写

  upper(x)         x 转为大写

  ltrim(x, trim_str)      把x左边截去trim_str字符串,缺省截去空格

  rtrim(x, trim_str)     把x右边截去trim_str字符串,缺省截去空格

  replace(x, old, new)     在x中查找old, 并替换为new

  substr(x, start, length)   返回x的字符串,从start处开始, 截取length个字符,缺省length,默认到结尾

1.2 数学函数

  abs(x)    x的绝对值

  ceil(x)   向上取整

  floor(x)   向下取整

  mod(x, y)  对x求y的余数

1.3 日期函数

  sysdate              当前系统时间

  current_date     返回当前系统日期

  add_months(d1, n1)  返回在日期d1基础上再加n1个月后新的日期

  last_day(d1)      返回日期d1所在月份最后一天的日期

  months_between(d1, d2) 返回日期d1到日期d2之间到月数

  next_day(d1, [c1])    返回日期d1在下周,星期几(参数c1)的日期

1.4 转换函数

  to_char(x, c)    将日期或数据x按照c的格式转换为char数据类型

  to_date(x, c)    将字符串x按照c的格式转换为日期

  to_number(x)   将字符串x转为数字型

1.5 常用组函数

  avg()   平均值

  sum()  求和

  min()   最小值

  max()    最大值

  count()  统计  

 注: null不参与运算

posted @ 2022-01-07 18:21  IT6889  阅读(75)  评论(0编辑  收藏  举报