录制使用的文件第2.2部分oracle11g复杂sql语句_第1页
录制使用的文件第2.2部分oracle11g复杂sql语句_第2页
录制使用的文件第2.2部分oracle11g复杂sql语句_第3页
录制使用的文件第2.2部分oracle11g复杂sql语句_第4页
录制使用的文件第2.2部分oracle11g复杂sql语句_第5页
已阅读5页,还剩50页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、2.2 复杂SQL语句和复杂函数的使用目标掌握 SQL语句的执行顺序、能分析复杂SQL语句的执行过程 掌握 Oracle子查询、自连接、行列转换掌握分析函数、decode函数、 SELECT CASE WHEN的使用掌握 Oracle 分页、删除重复记录的方法SQL语句的执行顺序 SQL语句的执行顺序 常见的select、from、where的顺序:1, from 2, where 3, select完整的select、from、where、group by、having、order by的顺序:1, from 2, where 3, group by 4,having 5, select 6

2、, order byEXISTS 的使用 EXISTS的使用 EXISTS用来判断查询所得的结果中,是否有满足条件的纪录存在。例:select *from student where exists(select * from address where zz=郑州); 从select 、from、where三者的先后执行顺序来分析。子查询的使用 子查询的使用 create table student(sno number(6) ,birthday date, sname varchar2(10);insert into student values(1, 11-1月-81 , 张三);inse

3、rt into student values(2, 10-3月-82 , 李四);insert into student values(3, 06-1月-83 , 王五);insert into student values(4, 26-1月-83 , 赵六);create table address(sno number(6) , zz varchar2(10);insert into address values(1, 郑州);insert into address values(2, 开封);insert into address values(3, 洛阳);insert into ad

4、dress values(4, 郑州); 子查询的使用 要求:找出zz是郑州的学生中,sno最大的学生的 sname 。 子查询的使用 select sname from student where sno =( select max(sno) from addresswhere zz= 郑州 );group by的练习 group by 练习 1.创建student表 create table student(xh number,xm varchar2(10),nl int); insert into student values (1,A,21); insert into student

5、values (2,B,22); insert into student values (3,A,23); insert into student values (4,A,24); insert into student values (5,A,25); insert into student values (6,C,26); insert into student values (7,B,27); group by 练习 任务:查找xm有相同的纪录,并显示出来,如下: XH XM NL- - - 1 A 21 3 A 23 4 A 24 5 A 25 2 B 22 7 B 27 group

6、by 练习答案:select * from student where xm in (select xm from student group by xm having count(*)1);自连接的使用 自连接的使用 CREATE TABLE 管理人员 ( 编号 char(10) , 姓名 varchar2(10) , 管理人员编号 char(10);insert into 管理人员 values(001, 张一, 004);insert into 管理人员 values(002, 张二, 004);insert into 管理人员 values(003, 张三, 003);insert i

7、nto 管理人员 values(004, 张四, 004); 自连接的使用 要求:现在想显示:编号,姓名,管理人员姓名 自连接的使用 select a.编号,a.姓名,b.姓名 as 管理人员姓名 from 管理人员 a join 管理人员 b on a.管理人员编号=b.编号;SELECT CASE WHEN的的使用 SELECT CASE WHEN的的使用语法:CASE WHEN 条件1 THEN action1 WHEN 条件2 THEN action2 WHEN 条件3 THEN action3 . ELSE actionN END CASE SELECT CASE WHEN的的使用s

8、elect case when substr(20090310,5,2) = 01 then 一月份 when substr(20090310,5,2) = 02 then 二月份 when substr(20090310,5,2) = 03 then 三月份 when substr(20090310,5,2) = 04 then 四月份 else null end from dual;SELECT CASE WHEN的的使用语法:CASE selector WHEN value1 THEN action1 WHEN value2 THEN action2 WHEN value3 THEN a

9、ction3 . ELSE actionNEND CASE SELECT CASE WHEN的的使用select case substr(20090310,5,2) when 01 then 一月份 when 02 then 二月份 when 03 then 三月份 when 04 then 四月份 else null end from dual; SELECT CASE WHEN的的使用create table 成绩(sno number, km varchar2(10), score number,grade char(6); insert into 成绩 values(1, 语文, 65

10、,null);insert into 成绩 values(2, 数学, 76,null);insert into 成绩 values(3, 英语, 86,null);insert into 成绩 values(4, 语文, 94,null);要求:把每个学生的grade列,用相应的等级来更新。SELECT CASE WHEN的的使用update 成绩 set grade = ( select grade from ( select sno , case when score = 90 then 优秀 when score = 80 then 良好 when score = 70 then 中等

11、 when score = 60 then 及格 else 不及格 end grade from 成绩 ) a where 成绩.sno = a.sno );复杂更新语句的使用复杂更新语句的使用表T1里有 a,b,c.N个字段,表T2里有 a,b,c三个字段,然后想在T1中c与表T2中c相同的情况下从表T2中将a,b覆盖表T1中的a,b怎么做 ? create table T1(a int ,b int ,c int ,d int ,e int); create table T2(a int ,b int ,c int ); insert into T1 values(1,2,3,4,5);

12、insert into T1 values(10,20,3,4,5); insert into T1 values(10,20,4, 40,50); insert into T2 values( -1, -1 , 3); insert into T2 values( -2, -2, 4);复杂更新语句的使用update t1 set a= (select a from t2 where t1.c= t2.c ) , b =(select b from t2 where t1.c= t2.c) where t1.c in (select c from t2);分析函数的使用分析函数 分析函数用于

13、计算完成聚集的累计排名、序号等 分析函数为每组记录返回多个行 以下三个分析函数用于计算一个行在一组有序行中的排位,序号从1开始 ROW_NUMBER 返回连续的排序,不论值是否相等 RANK 具有相等值的行排序相同,序数随后跳跃 DENSE_RANK 具有相等值的行排序相同,序号是连续的DECODE 的使用 DECODE 中的if-then-else逻辑 在逻辑编程中,经常用到If Then Else 进行逻辑判断。在DECODE的语法中,实际上就是这样的逻辑处理过程。它的语法如下:DECODE(value, if1, then1, if2,then2, if3,then3, . . . el

14、se )Value 代表某个表的任何类型的任意列或一个通过计算所得的任何结果。当每个value值被测试,如果value的值为if1,Decode 函数的结果是then1;如果value等于if2,Decode函数结果是then2;等等。事实上,可以给出多个if/then 配对。如果value结果不等于给出的任何配对时,Decode 结果就返回else 。需要注意的是,这里的if、then及else 都可以是函数或计算表达式。DECODE 的简单例子 Create table student(id number,name varchar2(10),sex char(1);Insert into

15、student values(1, 张, 1);Insert into student values(2, 王, 2);Insert into student values(3, 李, 1);Select name ,decode(sex, 1,男生, 2,女生) from student;DECODE 的简单例子 用case实现:select id,name, case sex when 1 then 男 when 2 then 女 end 性别from student; DECODE 取出一行内两列中的较大值 Create table sales(month char(2),sales_t

16、v puter number); Insert into sales values(01, 10, 18); Insert into sales values(02, 28, 20); Insert into sales values(03, 36, 33); DECODE 取出一行内两列中的较大值select month, decode(sign(sales_tv puter), 1, sales_tv, puter) as 较大销售量 from sales; Oracle中的行列转换行列转换1create table 销售(商品名称 varchar2(10), 季度 char(2), 销售

17、额 number);insert into 销售 values(电视机, 01, 100);insert into 销售 values(电视机, 02, 200);insert into 销售 values(电视机, 03, 300);insert into 销售 values(空调, 01, 50);insert into 销售 values(空调, 02, 150);insert into 销售 values(空调, 03, 180);格式1:商品名称 季度 销售额- - -电视机 01 100电视机 02 200电视机 03 300空调 01 50空调 02 150空调 03 180格式

18、2:商品名称 一季度 二季度 三季度 四季度- - - - -电视机 100 200 300 0空调 50 150 180 0从格式1到格式2:select a.商品名称, sum(decode(a.季度,01, a.销售额 ,0 ) 一季度, sum(decode(a.季度,02, a.销售额 ,0 ) 二季度, sum(decode(a.季度,03, a.销售额 ,0 ) 三季度, sum(decode(a.季度,04, a.销售额 ,0 ) 四季度 from 销售 a group by a.商品名称 order by 1;ROWNUM 的使用创建yggz表 create table yg

19、gz ( bh number(6) , gz number );insert into yggz values(1,1000);insert into yggz values(2,1100);insert into yggz values(3,900);insert into yggz values(4,2000);insert into yggz values(5,1500);insert into yggz values(6,3000);insert into yggz values(7,1400);insert into yggz values(8,1200);ROWNUM使用作用:对查

20、询结果,输出前若干条记录注意:只能与、=3 and rn = 5;或者:select * from yggz where rownum=5minus select * from yggz where rownum=2;ROWNUM使用任务:(2)按工资由高到底,查找表中,第3高的到第5高的记录,并显示出来。 结果如下: BH GZ - - 5 1500 7 1400 8 1200ROWNUM使用任务(2)答案: select bh,gz from ( select a.*,rownum rn from( select yggz.* from yggz order by gz desc ) a

21、) where rn2;或者: select * from (select * from yggz order by gz desc) where rownum=5 minus select * from (select * from yggz order by gz desc) where rownum 1) AND ROWID NOT IN (SELECT MIN(ROWID) FROM student GROUP BY sno HAVING COUNT(*) 1);删除重复记录 方法2DELETE FROM student WHERE ROWID IN (SELECT A.ROWID F

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论