oracle——学习之路(select检索)
select语法:
select [distinct|all] 列名 from 表名 [where] [group by] [having] [order by] ps:[] 表示可以省略
举几个栗子:
select * from emp; ps:* 表示所有字段即把要查询的表的所有字段都显示出来,并不建议使用因为网络消耗大,效率也不高

select empno from emp; ps:筛选指定字段,可以筛选多个,字段之间用逗号隔开

select empno,ename,job from emp; ps:筛选指定字段,可以筛选多个,字段之间用逗号隔开

select job from emp;

select distinct(job) from emp; ps:all是默认的即有重复也会显示,distinct消除重复

select * from emp where comm is not null; ps:null表示空值

select job,count(*),sum(sal) from emp group by job; ps:count() sum()为聚合函数后面会讲

select sum(sal), count(*), (sum(sal)/count(*)) from emp group by job having (sum(sal)/count(*))>2000; ps:having 要与group by 连用 having 要放在group by后面,group by 可以单独使用

select * from emp order by empno desc; ps:desc是按降序排序,asc是按升序排序,默认是asc

select empno as 雇员编号 from emp order by 雇员编号; ps:雇员编号是别名,别名中英文不限,当然你要用阿拉伯语,德语什么的只要可以打出来识别也没问题,as可以省略也可以写,都是一样的道理,order by后应该使用别名,只有order by 可以这样,having后面都不可以

select * from emp order by empno desc ,job; ps:可以根据两个字段进行排序,先按照empno进行降序排序,如果相等对job进行升序排序

select job,sum(sal) from emp group by job ; ps:group by 后的字段必须在查询字段出现,查询字段还可以出现聚合函数

select job,sum(sal)*2 from emp group by job ; ps:不表示数据库的数据被改变只表示显示的结果

select ename ,sal from emp where sal not between 4000 and 5000; ps:between 4000 and 5000 相当于 >=4000 and <=5000

select job , ename from emp where sal in(800,1600,1500); ps:在集合中选取符合条件的

select ename from emp where ename like '__A%'; ps:模糊查询,_代表匹配一个字符,%代表匹配至少0 个字符

select ename from emp where ename like '%A%' or ename like '%E%';

使用where来进行筛选时,常用的操作符:< 小于 >大于 = 等于 !=不等于 <>不等于 <=小于等于 >=大于等于
having是对group分的组进行筛选,不同于where group要分的组是where筛选过后的
子查询:
select empno, ename from emp where empno in (select empno from emp where comm is not null);

any 和<any <=any表示小于或小于等于列表中最大值,与 in配合使用 和> any >=any表示大于或大于等于列表中的最小值 =any 相当于in
all 和<all <=all表示小于或小于等于列表中的最小值,与in配合使用 和>all >=all表示大于或大于等于列表中的最大值 <>all相当于 not in
举几个栗子:
select sal from emp where comm is not null;(1)

select * from emp where sal =any(select sal from emp where comm is not null);(2)

select * from emp where sal in(select sal from emp where comm is not null);(3)

select * from emp where sal <any(select sal from emp where comm is not null);

select * from emp where sal <=any(select sal from emp where comm is not null);

select * from emp where sal >any(select sal from emp where comm is not null);

select * from emp where sal >=any(select sal from emp where comm is not null);

select * from emp where sal <>all(select sal from emp where comm is not null);

select * from emp where sal >all(select sal from emp where comm is not null);

select * from emp where sal >=all(select sal from emp where comm is not null);

select * from emp where sal <all(select sal from emp where comm is not null);

select * from emp where sal <=all(select sal from emp where comm is not null);

注意看这几句的关系
连接查询:
select * from emp, dept; 产生笛卡儿积

内连接:等值连接、不等值连接
等值连接:连接中使用“=”(等号) 连接两个条件列表
select * from emp e, dept d where e.deptno=d.deptno; select * from emp e inner join dept d on e.deptno=d.deptno; //功能相等 ps:inner可以省略,系统自动识别为内连接

不等值连接:连接时使用<、 > 、>=、 <=、 between ……and ……、in等连接两个条件列表
自连接:把自身表的一个引用作为另一个表来处理
select e.empno 雇员编号, e.ename 雇员姓名,m.empno 领导编号 from emp e, emp m where e.mgr=m.empno;

外连接:左外连接、右外连接、全外连接
左外连接:返回结果不仅仅符合连接条件的行记录,左表全部记录都会包含,右表不满足的用NULL填充
右外连接:返回结果不仅仅符合连接条件的行记录,右表全部记录都会包含,左表不满足的用NULL填充
全外连接:无论是否成功匹配,左右表记录都返回,不满足用NULL填充
oracle使用外连接有一种特殊的方法,用(+)表示外连接,放在非主表的一方
举几个栗子:
dept是左表,采用左外连接
select d.deptno, d.dname,a.empno, a.ename,a.job,a.sal from dept d ,(select empno, ename, job ,sal ,deptno from emp where comm is null) a where d.deptno=a.deptno(+);
select d.deptno, d.dname,a.empno, a.ename,a.job,a.sal from dept d left join (select empno, ename, job ,sal ,deptno from emp where comm is null) a on d.deptno=a.deptno;
以上两个查询语句相等

采用右外连接:
select d.deptno, d.dname,a.empno, a.ename,a.job,a.sal from dept d right join (select empno, ename, job ,sal ,deptno from emp where comm is null) a on d.deptno=a.deptno;
select d.deptno, d.dname,a.empno, a.ename,a.job,a.sal from dept d , (select empno, ename, job ,sal ,deptno from emp where comm is null) a where d.deptno(+)=a.deptno;
以上两个查询语句相等

采用全外连接:
select d.deptno, d.dname,a.empno, a.ename,a.job,a.sal from dept d full join (select empno, ename, job ,sal ,deptno from emp where comm is null) a on d.deptno=a.deptno;

献给和我一样的小白,有关查询还有很多知识点,这里只写了常用的,如有错误请指出,谢谢!
oracle——学习之路(select检索)的更多相关文章
- [原创]java WEB学习笔记92:Hibernate学习之路-- -QBC 检索和本地 SQL 检索:基本的QBC 查询,带 AND 和 OR 的QBC,统计查询,排序,分页
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记90:Hibernate学习之路-- -HQL检索方式,分页查询,命名查询语句,投影查询,报表查询
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记89:Hibernate学习之路-- -Hibernate检索方式(5种),HQL介绍,实现功能,实现步骤,
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记88:Hibernate学习之路-- -Hibernate检索策略(立即检索,延迟检索,迫切左外连接检索)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Oracle 学习之路开始
今年刚毕业,从毕业到现在工作正式工作四个多月了(实习的几个月就不算了).工作之中遇到的困难不少,学到的东西也不少:但是感觉学到的东西还是不够,毕竟java水很深啊. 现在工作中并不是每天都能学到新的东 ...
- oracle学习之路(四) ---------PL/SQL 表,二维数组(TABLE)
LOB类型 ORACLE提供了LOB (Large OBject)类型.用于存储大的数据对象的类型.ORACLE眼下主要支持BFILE, BLOB, CLOB 及 NCLOB 类型. NCLOB 存储 ...
- oracle学习之路(二)------数组类型/记录类型的使用
Oracle记录类型介绍 RECORD:用户自己定义数据类型,由单行多列的标量构成的复合数据类型.它将一个或多个标量封装成一个对象进行操作记录不能够总体拿来比較也不能够总体推断为空.能够总体拿来赋值. ...
- oracle——学习之路(oracle内置函数)
oracle与很多内置函数,主要分为单行函数与集合函数. 首先要提一下dual表,它oracle的一个表,没有什么实质的东西,不能删除它,否则会造成Oracle无法启动等问题,他有很大用处,可以利用它 ...
- Oracle学习之路-- 案例分析实现行列转换的几种方式
注:本文使用的数据库表为oracle自带scott用户下的emp,dept等表结构. 通过一个例子来说明行列转换: 需求:查询每个部门中各个职位的总工资 按我们最原始的思路可能会这么写: ...
随机推荐
- junit3和junit4的使用区别如下
junit3和junit4的使用区别如下1.在JUnit3中需要继承TestCase类,但在JUnit4中已经不需要继承TestCase2.在JUnit3中需要覆盖TestCase中的setUp和te ...
- 区间DP小结 及例题分析:P1880 [NOI1995]石子合并,P1063 能量项链
区间类动态规划 一.基本概念 区间类动态规划是线性动态规划的拓展,它在分阶段划分问题时,与阶段中元素出现的顺序和由前一阶段的那些元素合并而来由很大的关系.例如状态f [ i ][ j ],它表示以已合 ...
- tarjan求强连通分量(模板)
https://www.luogu.org/problem/P2341 #include<cstdio> #include<cstring> #include<algor ...
- Django基础之ORM操作
################################################################## # PUBLIC METHODS THAT ALTER ATTRI ...
- 二十、网络ifconfig 、ip 、netstat、ss之二
ip 网络层协议 ip地址 点分十进制分为4段,范围 0-255 ip分类 A 占据1段,最左侧一段第一位固定为0 0 000 0000 - 0 111 1111 0 - 127:其中0为网络,12 ...
- CF1214A
CF1214A 题意: 有n个卢布,要换成美元和欧元,使手上剩余的卢布最少.一美元价值d卢布,一欧元价值e卢布. 解法: 可以看成只有两个没有代价的可以无限取的物品的完全背包. CODE: #incl ...
- Linux下 Java 读取文件路径
一般文件路径在windows中用 \ 表示,但是在其他系统平台下比如linux中就不是 \ 所以java给我们提供了一个与平台无关的表示路径的常量 File.separator在windows中则表示 ...
- JAVA基础知识|反射
一.理解反射 1.1.基础概念 反射:在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为ja ...
- Golang字符串处理以及文件操作
一.整数 1.int与uint的初值比较以及其大小. 1 /* 2 #!/usr/bin/env gorun 3 @author :xxxx 4 Blog:http://www.cnblogs.com ...
- 【面试题总结】1、统计字符串中某个字符出现的次数(2-Python实现)
1.可以使用Python的字典实现,对于一个特定的字符串,使用for循环遍历其中的字符,并保存成字典形式.字典的key为字符,value为字符在整个字符串中出现的次数. 2.拓展:如果题目为比较两个字 ...