当前位置:网站首页>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;
边栏推荐
- Dingding conflicts with RStudio shortcuts--Dingding shortcut settings
- B. Arrays Sum
- 【Harmony OS】【ArkUI】ets开发 创建视图与构建布局
- [21天学习挑战赛——内核笔记](四)——内核常见调试手段(printf、dump_stack、devmem)
- 算法---优美的排列(Kotlin)
- A case of missing heritability
- 基于ABP和Magicodes实现Excel导出操作
- php uses phpoffice/phpspreadsheet to import and export excel tables
- 杰理之电话打入,远端听不到声音【篇】
- [Harmony OS] [ARK UI] ETS context basic operations
猜你喜欢
杰理之电话打入,远端听不到声音【篇】
mysql内容不存在的报错
基于ABP和Magicodes实现Excel导出操作
Alibaba Cloud Tianchi Contest Question (Machine Learning) - Prediction of Industrial Steam Volume (Complete Code)
【Harmony OS】【FAQ】鸿蒙问题合集1
Alibaba Cloud Tianchi Contest Question (Machine Learning) - Repeat Purchase Prediction of Tmall Users (Complete Code)
亚马逊面对风控,自养号测评时应该怎么做?
通讯录(文件版)(C语言)(VS)
LeetCode-636. 函数的独占时间
换座位[异或巧妙的让奇偶互换]
随机推荐
ABP 6.0.0-rc.1的新特性
杰理之电话打入,远端听不到声音【篇】
php使用phpoffice/phpspreadsheet导入导出excel表格
Masked AutoEncoder论文及实现
[UNR #6 A] Noodle-based road (shortest path)
【开发者必看】【push kit】推送服务服务典型问题合集2
JS-DOM-对象的事件onload、匿名函数、this
Ali YunTianChi competition problem (deep learning) - video enhancement (complete code)
GraalVM安装
【Harmony OS】【FAQ】Hongmeng Questions Collection 1
Cluster deployment using ceph-deploycep with 3 disks as dedicated osd
2022年8月深圳产品经理认证招生简章(NPDP)
通讯录(动态版)(C语言)(VS)
Efficient review of deep learning DL, CV, NLP
[OpenCV] - Find and draw contours
y91.第六章 微服务、服务网格及Envoy实战 -- 服务网格基础(二)
关于sys.path.append(‘..‘)失效
说明高级语言、汇编语言、机器语言三者的区别,谈谈你对汇编语言的认识。
2022下半年深圳信息系统项目管理师认证招生简章
LeetCode - remove consecutive nodes with a sum of zero from a linked list