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;
献给和我一样的小白,有关查询还有很多知识点,这里只写了常用的,如有错误请指出,谢谢!
最新文章
- 通过GitHub Pages建立个人站点总结与体会
- Linux系统下配置Tomcat
- 类的继承和多态性-编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 getLegs(),设置动物名称的方法 setKind(),获得动物名称的方法 getKind(),获得动物数量的方法 getCount()。定义Fish类,是Animal类的子类,
- JavaScript break跳出多重循环
- 编写高质量JS代码的68个有效方法(十三)
- VS - 实用技巧
- win10安装软件被阻止后
- bzoj3064 CPU监控
- 了解运行时类型信息(RTTI)
- Android学习----Android Studio 技巧汇总
- Linq的Distinct太不给力了[转]
- dubbo 请求调用过程分析
- jmeter之beanshell提取json数据
- 关于 SVN 项目检出
- Guava 教程1-使用 Google Collections,Guava,static imports 编写漂亮代码
- JS实现 阿拉伯数字金额转换为中文大写金额 可以处理负值
- 第一次冲刺意见汇总&;团队第一阶段总结
- 命令行编译多个java文件
- Map Labeler POJ - 2296(2 - sat 具体关系建边)
- python构建bp神经网络_鸢尾花分类(一个隐藏层)__2.代码实现
热门文章
- 运算符重载之new与delete
- Codeforces 1180E Serge and Dining Room
- pymysql基本使用
- 内存管理2-@class关键字
- https服务
- Python学习日记(八)—— 模块一(sys、os、hashlib、random、time、RE)
- POJ 3342 Party at Hali-Bula ——(树型DP)
- JAVA基础知识|Socket
- encode &;&; decode &;&; 加密 &;&;解密
- P5657 格雷码【民间数据】