Oracle基本查询语言

Oracle


基本查询:

-- 基本查询
/*
DDL:数据库定义语言   :create 、 drop
DML:数据库的操作语言 :insert、update、delete
DQL:数据库的查询语言 :select 【重点】
DCL:数据库的控制语言 :grant 、revoke
*/
--查询员工表
select * from emp;
select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp;
--别名:一般不用中文   ,不能使用单引号 ,别名如果有数字,或者数字开头 必须用双引号套起来
select empno 员工编号,ename as 员工姓名,job "工作",mgr as "上级领导",hiredate "123456"  from emp;
--去重
select distinct job from emp;
--四则运算 : + - * /
--查询员工的年薪
select empno,ename, sal*12 as 年薪 from emp;
--查询员工的年收入
select empno,ename,sal,comm, sal*12+comm as 年收入 from emp;
--处理空值
-- nvl(v1,v2)   -- 当v1为空的时候,返回v2
select empno,ename,sal,comm, sal*12+nvl(comm,0) as 年收入 from emp;
--字符串拼接
/*
dual : 伪表,不存在的表,是为了补全语法而存在的表
concat(v1,v2)  : 把v1与v2 拼接起来
*/
select concat(concat('a','b'),'c') from dual;
--推荐用下面这个
select 'a'||'b'||'cWEFasfaSFsdf'  from dual;

条件查询:

/*
select * 
from 表名
where 条件
order by 字段 asc|desc
*/
--查询每月能得到奖金的雇员
--空值比较特殊: is null   |  is not null
select * 
from emp
where comm is not null and comm >0;
--使用了大于号 可以去空
select * 
from emp
where  comm >0;
--范例:基本工资大于 1500 但是小于 3000 的全部雇员
select *
from emp
where sal > 1500 and sal < 3000;
--范例:基本工资大于等于 1500 但是小于等于 3000 的全部雇员
select *
from emp
where sal >= 1500 and sal <= 3000;
--使用between
select *
from emp
where sal between 1500 and 3000;
--模糊查询
--查询名字中有 M 字母的用户
select * from emp where ename like '%M%';
--查询名字中第二个字母是 M 的
select * from emp where ename like '_M%';

排序查询:

 /*
order by 字段1 asc|desc , 字段2 asc|desc
        asc :默认值 升序,由小到大
        desc: 降序,由大小
*/
--查询员工表的数据,工资由大到小排序
select * 
from emp
order by sal desc;
--查询员工的数据,对奖金进行排序
--排序的时候处理空值:   nulls first | nulls last
select * 
from emp
order by comm desc nulls last;

单行函数:

/*
  处理的是一条数据,返回一条数据
  dual : 伪表
*/
--1、字符函数
--1.1 大小写
select upper('abc') from dual;
select lower('ABC') from dual;

--1.2 截取字符串: substr(v1,p1,p3)  :v1: 源字符串,p1:从哪里开始截取【0和1都是从第一个元素开始】,p2:截取几个
select substr('hello',0,2) from dual; -- he【0和1都是从第一个元素开始】
select substr('hello',1,2) from dual; -- he【0和1都是从第一个元素开始】
select substr('hello',2,2) from dual; -- el

--1.3 替换字符串: replace(v1,p1,p2)  : v1:源字符串,p1:要替换的字符串 ; p2: 替换之后的那个字符串
select replace('hello','l','x') from dual;

--2、数值函数
--2.1 四舍五入 round(v1,p1) : v1 :源数字,p1:保留小数位,如果没有p1,默认只保留整数
select round(5699.863) from dual; -- 5700
select round(5699.863,2) from dual; --5699.86
--2.2 截断 : trunc(v1,p1) : v1:源数字,p1: 保留小数位,如果没有p1,默认只保留整数,它截断,不会四舍五入
select trunc(5699.863) from dual; -- 5699
select trunc(5699.866,2) from dual; --5699.86

--3、日期函数
--3.1 当前系统时间 : sysdate
select sysdate from dual;

--3.2 计算员工入职的天数
select empno,ename,hiredate, trunc(sysdate-hiredate) 入职天数 
from emp;

--3.2 计算员工入职的周数
select empno,ename,hiredate, trunc((sysdate-hiredate) /7 )入职周数 
from emp;

--4、转换函数
--4.1 to_char ;转成字符串
--数字转字符串
select 123,to_char(123) from dual;
--日期转成字符串
select sysdate,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

--4.2 转成数字;to_number
select '123456',to_number('123456') from dual;

--4.3 转成日期:to_date()
select '2018-08-01 10:20:30',to_date('2018-08-01 10:20:30','yyyy-mm-dd hh24:mi:ss') from dual;

--5、通用的函数
--nvl     concat

--6、条件表达式
--6.1 case when :  sql99标准
-- 案例:查询员工表数据,根据部门的编号显示中文:当是10的时候,显示java部门;当是20的时候,显示 UI部门;其他的都显示 总裁办公室
select empno,ename,deptno,
       case deptno
          when 10 then 'java部门' 
          when 20 then 'UI部门'
          else
            '总裁办公室'
       end as "部门名称"
from emp;

--6.2 oracle独有的条件表达式: 
-- decode(v1,p1,p2,p3,p4.....pn) : 当v1等于p1的时候,显示p2;当v1等于p3的时候,显示p4;其他的情况就是最后一个pn;
select empno,ename,deptno,
       decode(deptno,10,'java部门',20,'UI部门','总裁办公室') 
       as "部门名称" from emp;

多行函数(聚合函数):

--  count  avg    sum   max   min 

select count(*) from emp;
select count(empno) from emp;

--求公司的平均工资
select avg(sal) from emp;

--求每个部门的平均工资
select deptno,avg(sal) 
from emp
group by deptno;

--求每个部门的平均工资,要求大于2000的才显示
select deptno,avg(sal) 
from emp
group by deptno
having avg(sal) > 2000;

-- where 和 having 区别: where后面不能加聚合函数

猜你喜欢

转载自blog.csdn.net/qq_42856618/article/details/81389591