当前位置:网站首页>Oracle02-sql学习(单表查询,exists子查询,函数入门)
Oracle02-sql学习(单表查询,exists子查询,函数入门)
2022-08-09 04:53:00 【魔人Majin Buu】
查询数据库中所有的表
-- 查询所有表
select * from tab;
select * from user_tables;
-- 查看表中的字段信息
-- 命令行方式中执行:desc emp;
表注释和字段注释
-- 给表添加注释
comment on table emp is '雇员信息表';
-- 给字段添加注释
comment on column emp.ename is '员工姓名';
查询语句DQL
-- 查询emp表中所有数据
select * from emp;
-- 查询部门编号为10的员工的信息
select * from emp where deptno = 10;
-- 去重查询 关键字distinct
select distinct * from emp;
别名(可以给列添加别名,也可以给表添加别名)
-- 给表和字段起别名
select e.empno as 员工ID, e.ename as 员工姓名
from emp e
where e.ename = 'KING';
-- as可以省略
select e.empno 员工ID, e.ename 员工姓名
from emp e
where e.ename = 'KING';
where条件判断
各种关系运算符(< > = != <> <= >=)
pass
some,any,all
-- any:代表其中的任何一个满足即是满足
select * from emp where sal = any(1000, 2000, 3000);
-- some:和any差不多
select * from emp where sal = some(1000, 2000, 3000);
-- all:满足全部即满足
select * from emp where sal = all(1000, 2000, 3000);
is null和is not null
pass
between and
-- between and
select * from emp where sal between 1000 and 3000;
in和not in
select * from emp where sal in (1000, 3000);
select * from emp where sal not in (1000, 3000);
and和or的优先级
and的优先级要高于or,所以or操作的时候要使用括号.
exitst(sub-query)子查询
比如查询部门编号为10或者20的员工信息
select *
from emp
where exists (select deptno
from dept
where (deptno = 10 or deptno = 20)
and dept.deptno = emp.deptno);
可以当成外循环与条件判断的关系,exists子查询就是条件,外部每次遍历的结果如果满足子查询结果即满足.
模糊查询like和escape()
-- 模糊查询
select * from emp where ename like 'S%';
-- escape转义指定的符号和字符,比如查询姓名中带有%号的雇员信息
select * from emp where ename like '%/%%' escape('/');
like的效率问题:低,尽量别用
练习题
-- 练习
-- 查询部门编号为10的员工信息
select * from emp where deptno = 10;
-- 查询年薪大于3w的员工信息
select * from emp where sal * 12 + nvl(comm, 0) > 30000;
-- 查询comm为null的员工信息
select * from emp where comm is null;
-- 查询sal大于1500并且有comm的员工信息
select *
from emp
where sal > 1500
and comm is not null;
-- 查询sal大于1500或者有comm的员工信息
select *
from emp
where sal > 1500
or comm is not null;
-- 查询姓名中包含S的员工信息
select * from emp where ename like '%S%';
-- 查询姓名以J开头,第二个字母为O的员工信息
select * from emp where ename like 'JO%';
-- 查询姓名中包含%的员工信息
select * from emp where ename like '%/%%' escape('/');
-- 查询部门名称为SALES和RESEARCH的雇员信息
-- 方式1:子查询
select *
from emp
where deptno in (select deptno
from dept
where dname = 'SALES'
or dname = 'RESEARCH');
-- 方式2:关联查询
select *
from emp e, dept d
where e.deptno = d.deptno
and (d.dname = 'SALES' or d.dname = 'RESEARCH');
-- 方式3:left join
select *
from emp
left join dept
on emp.deptno = dept.deptno
where dname in ('SALES', 'RESEARCH');
-- 方式4:exists()
select *
from emp
where exists (select *
from dept
where dname in ('SALES', 'RESEARCH')
and emp.deptno = dept.deptno);
排序 order by
1,数字顺序
2,字段顺序
-- order by 效率问题
select * from emp order by sal asc;
select * from emp order by sal desc;
order by存在效率问题,当需要执行这些业务操作时,尽量在业务低峰时期执行.
全集,并集,交集,差集
union all:包含两个集合的所有(不去重)
union:包含两个集合的所有(去重)
intersect:包含两个集合都有的元素
minus:仅包含A集合中有的元素
-- 全集
select *
from emp
where sal is null
union all
select *
from emp
where sal is not null;
-- 并集
select *
from emp
where sal is null
union
select *
from emp
where sal is not null;
-- 交集
select *
from emp
where sal is null
intersect
select *
from emp
where sal is not null;
-- 差集
select *
from emp
where sal is null
minus
select *
from emp
where sal is not null;
练习
--练习
-- 查询dept表中所有部门的名称
select dname 部门名称 from dept;
-- 查询emp表显示所有员工姓名以及其全年收入,并指定列名称为年收入
select ename 姓名, (sal + nvl(comm, 0)) * 12 年收入 from emp e;
-- 查询不存在雇员的所有部门编号
select dept.deptno
from dept
where not exists (select * from emp where emp.deptno = dept.deptno);
-- 查询emp表,显示工资超过2850的员工姓名和工资
select e.ename, e.sal from emp e where e.sal > 2850;
-- 查询工资不在1500~2850的员工信息
select * from emp where sal not between 1500 and 2850;
-- 查询empno为7566雇员姓名以及部门代码
select ename, deptno from emp where empno = 7566;
-- 查询部门编号为10和30中工资超过1500的雇员信息
select * from emp where sal >1500 and deptno = any(10,30);
-- 查询员工姓名第二个字符为A的员工信息
select * from emp where ename like '_A%';
-- 查询comm不为null的所有员工信息
select ename,comm from emp where comm is not null;
-- 获取所有姓名,工资,入职时间,并以雇员名称升序排列
select ename,sal,hiredate from emp order by ename asc;
-- 获取指定日期入职的雇员信息
select ename, job, hiredate
from emp
where hiredate between to_date('1981/2/1', 'yyyy-mm-dd') and
to_date('1981/5/1', 'yyyy-mm-dd')
order by hiredate;
-- 查询所有有补助的员工姓名,工资,补助,并按照工资升序,补助降序排列
select ename, sal, comm
from emp
where comm is not null
order by sal asc, comm desc;
Oracle函数
总体分类:
- 1,聚合函数:根据多条记录,获取单一结果,比如sum() ,count()
- 2,单行函数:根据单一记录,获取单一结果
单行函数的分类:
- 字符函数
- 数值函数
- 日期函数
- 转换函数
- 其他函数
字符函数
-- 字符函数
-- 字符串拼接
select concat('ename is ', ename) from emp;
-- 首字母大写
select initcap(ename) from emp;
-- 全部大写
select upper(ename) from emp;
-- 全部小写
select lower(ename) from emp;
-- 左/右填充
select lpad(ename,10,'*') from emp;
select rpad(ename,10,'*') from emp;
-- 去除空格(两边,左边,右边)
select trim(ename) from emp;
select ltrim(ename) from emp;
select rtrim(ename) from emp;
-- 获取字符出现的位置(从1开始)
select instr('weiwenlong','w') from dual;
select instr(ename,'A') from emp;
-- 查看字符串的长度
select ename, length(ename) from emp;
-- 子串获取
select substr(ename,0,3) from emp;
-- 替换操作
select replace('weiwenlong','wei','666') from dual;
数值操作
-- 四舍五入 round(num,保留小数点后几位)
select round(3.1415926,2) from dual;
select round(3.1415926,3) from dual;
-- 截断数据 trunc(num,保留小数点后几位)
select trunc(3.1415926,2) from dual;
select trunc(3.1415926,3) from dual;
-- 取模
select mod(9,2) from dual;
-- 向上取整/向下取整
select ceil(3.14) from dual;
select floor(3.14) from dual;
边栏推荐
猜你喜欢
LeetCode - remove consecutive nodes with a sum of zero from a linked list
Quantitative Genetics Heritability Calculation 2: Half Siblings and Full Siblings
Faced with risk control, what should Amazon do when evaluating self-supporting accounts?
安装pytorch和cuda
[21天学习挑战赛——内核笔记](四)——内核常见调试手段(printf、dump_stack、devmem)
稳定性测试怎么做,这篇文章彻底讲透了!
Alibaba Cloud Tianchi Contest Question (Machine Learning) - Prediction of Industrial Steam Volume (Complete Code)
【Harmony OS】【ARK UI】Date 基本操作
How to do the stability test, this article thoroughly explains it!
抖音直播新号怎么起号?抖音直播间不进人怎么办?
随机推荐
存储系统架构演变
HP路由器和交换机日志分析
ddr系统检验
数字化时代,企业为什么需要商业智能BI
Base64编码和图片转化
稳定性测试怎么做,这篇文章彻底讲透了!
【HMS core】【ML kit】机器学习服务常见问题FAQ
AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline'
JS-DOM--全局DOM对象document-history-location-navigator-screen-window
匿名共享内存 ashmem
LeetCode - remove consecutive nodes with a sum of zero from a linked list
【暑期每日一题】洛谷 P5724 【深基4.习5】求极差 / 最大跨度值
【HMS core】【ML kit】Machine Learning Service FAQ
抖音直播间带货最新玩法和运营技巧
抖音直播带货的4个技巧,提升直播间转化率!
GraalVM安装
如何选型APS系统,还需明确这七大关键因素
equals and ==
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarExcept
[21天学习挑战赛——内核笔记](四)——内核常见调试手段(printf、dump_stack、devmem)