使用前提
- 有mysql基础,熟练使用常见增删改查
- 已经安装完Oracle和PL/SQL
- 使用scott用户(tiger)自带的emp表和表里面的数据
简单语法使用
any:表示满足其中一个的意义
另外, =any 的操作结果类似于in
select * from emp where sal > any(3000,4000,5000) and comm>500
-- in() 等同于 =any()
select * from emp where sal in(3000,4000,5000)
dual: 是一张哑表,不查询任何的数据
主要用于计算
select * from dual;
select 3+5 from dual;
Like进行模糊查询:
‘-’ 表示匹配任意一个字符
‘%’ 表示匹配任意多个字符
select * from emp where ename like '张%'
between and: 进行范围筛选( >= ? and <= ?)
select * from emp where sal between 3000 and 5000;
select * from emp where sal >= 3000 and sal <= 5000;
-- 查询雇佣日期在1982.1.1到1982.10.10的员工的信息
select * from emp where hiredate between to_date('1982-1-1','YYYY-MM-dd') and to_date('1982-10-10','YYYY-MM-dd');
-- 查询员工表中在1981年中雇佣的员工信息
select * from emp where hiredate between to_date('1981-01-01','yyyy-MM-dd') and to_date('1981-12-31','yyyy-MM-dd');
is null关键字: 就是为空就符合条件
-- 所有没有奖金的员工信息
select * from emp where comm = 0 or comm is null;
order by关键字(asc: 升序,默认)
select * from emp order by comm
伪列
只能从1开始数(参考游标),之所以这样,有这种情况是因为rownum并不是真实存在的列
使用RowNum: 伪列进行分页
主要是用在嵌套内部类里面进行分页
select rownum,e.* from emp e where rownum <= 5;
-- 如果不想伪列的值从1-N,想从X-N的话,只能使用子查询
select rownum,ename from scott.emp where rownum>3 and rownum<10 -- 查询不出来数据
select * from (select rownum rw,e.ename from scott.emp e) a where a.rw > 3 and a.rw < 10
使用RowId: 伪列得到计算机物理地址
select e.*,rowid from emp e;
排序训练
查询员工的信息,按月薪降序排序,如果月薪相同,则按雇佣日期降序排序
select * from emp order by sal desc,hiredate description;
查询员工的姓名,年薪,按年薪升序排列
select ename,12*sal yearSalary from emp order by yearSalary asc;
查询出部门编号为30的员工信息,按编号降序排序
select * from emp where deptno = 30 order by empno desc
单行函数
- 对比:
单行函数(一行数据出一行结果)
聚合函数(多行数据出一行结果)
返回指定字符ASCII码
select ascii('A') from dual;
找到从下标为5的位置开始的第二个’we’
select instr('abwecbgjbwehjhjwejhwe','we',5,2) from dual;
把字符串两边的’e’字符给截取了
默认是去空格
select trim('e' from ' abwecbgjbwehjhjwejhwe') from dual
-- 去空格
select trim(' abwecbgjbwehjhjwejhwe ') from dual
nvl(x,value): 如果值x为null,就给他赋值为value
select ename,sal,nvl(sal,0) + nvl(comm,0) from emp;
nvl2(x,value1,value2): 如果值x不为null,就给他返回value1,否则返回value2
select ename,sal,nvl2(sal,sal,0)+nvl(comm,0) from emp;
日常练习
-- 查询出所有员工的姓名,首字母大写,其他字母小写
select initcap(lower(ename)) from emp;
-- 查询出所有员工的名字,如果姓名中有“S",全部替换成"8"
select replace(ename,'S','8') from emp;
-- 查询出姓名中有两个“L"的员工信息
select * from emp where ename like '%L%L%';
-- 查询出所有的员工的姓名和职位的前5个字符,取别名要加双引号
select substr(ename,1,5) as "name",substr(job,1,5) as "job" from emp
select substr(ename,1,5) as name,substr(job,1,5) as job from emp
-- 查询姓名字符数超过五个的员工信息
select * from (select length(ename) as elength,e.* from emp e) where elength > 5;
select * from emp where length(ename) > 5
-- 查询出所有员工的姓名和年总收入
select ename,12*(nvl(sal,0)+nvl(comm,0)) from emp
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/202516.html