oracle学习之--高级查询基础

来源:互联网 发布:js如何实现多重继承 编辑:程序博客网 时间:2024/04/29 07:52

今天学习sql中的重点---使用SQL进行多表查询.所谓的多表查询是相对单表而言的,指丛多个数据表中查询数据。
如今的数据库的主流仍然是关系型数据库,所谓关系型,顾名思义就是表之间是有相互联系的,在实际项目中,SQL语句使用的重点和难点就是多表查询,(有些查询语句甚至多达几十,上百行),。。。。开始我们的高级查询学习之路吧。
1)无条件多表查询 --是将各表的记录以“笛卡尔”积的方式组合起来如scott.dept表有4条记录,scott.emp 有14条记录经过
select emp.empno,emp.ename,emp.deptno,dept.dname, dept.loc
from scott.emp, scott.dept; 一共有4×14 = 56条记录(很多查询都是使用了笛卡尔积的方式进行查询,大家一定要看清楚到底是怎么等到结果的,简单的说就是scott.dept里面的每条记录去与scott.emp里面的记录去匹配)
2)等值多表查询 --等值查询就象无条件查询然后增加一个过滤器,过滤掉所有不符合条件的数据example:select emp.empno,emp.ename,emp.deptno,dept.dname, dept.loc from scott.emp,scott.dept where scott.emp.deptno = scott.dept.deptno;oracle中对等值的检查很严格,要求关联的数据库的某些字段具有相同的数据类型,宽度,和取值范围(过滤器只是方便大家理解使用来说明的,实际数据库的运行情况是不同的)
3)非等值多表查询 --与等值查询相反的操作 就丢个sql语句到这里吧!
example:select  emp.empno, emp.ename, emp.deptno, dept.dname , dept.loc
from scott.emp, scott.dept
where  scott.emp.deptno!=scott.dept.deptno and scott.emp.deptno=10;
下面再来一种很经常使用的查询方法:SQL嵌套查询 --我们习惯的叫法是子查询.
子查询的意识是将一个select语句的结果当成父查询的条件,子查询可以嵌套多层,子查询操作的数据表可以是父查询中不操作的表,也可以是。另外子查询中不能有order by 分组语句。下面我们来看下子查询的几中方式
1)简单的子查询 examplale:
select  emp.empno,emp.ename,emp.job,emp.sal 
from scott.emp
where sal>=(select sal from scott.emp where ename='WARD');
select sal from scott.emp where ename='WARD'将返回一个数字,假设为1000
然后 select  emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal>1000得到我们需要的结果。
2)带in的查询语句
上面的查询中我们返回的是一个数字 1000,很多时候查询的结果是一个结果集合,那么我们就要使用 in 查询来嵌套查询 example:
select  emp.empno,emp.ename,emp.job,emp.sal 
from scott.emp
where sal in (select sal from scott.emp where ename='WARD');相应的也可以用not in来查询
3)带any的子查询 example:
select  emp.empno,emp.ename,emp.job,emp.sal 
from scott.emp
where sal >any(select sal from scott.emp where job='MANAGER');查询大于子结果集中任意一个元素的结果
4)带some的子查询 example:
select  emp.empno,emp.ename,emp.job,emp.sal 
from scott.emp
where sal =some(select sal from scott.emp where job='MANAGER');查询等于子结果集中的任意一个元素,some子查询和any查询的功能基本一致。
5)带all 的子查询 example:
select  emp.empno,emp.ename,emp.job,emp.sal 
from scott.emp
where sal >all(select sal from scott.emp where job='MANAGER');查询大于子结果集中的所有元素
6)from 语句中的子查询 子查询不一定都是出现在where 语句中,也可以出现在from语句里面当查询表使用。example:
select  emp.empno,emp.ename,emp.job,emp.sal 
from scott.emp ,(select sal from scott.emp where job='MANAGER')  salemp
where emp.sal = salemp.sal
7)相关子查询 相关子查询是一种很特殊的查询方式,普通的子查询的结果只计算一次,然后将他的查询结果返回给高层查询,相关子查询要求子查询计算多次,每次付给查询中的条件随着高层关联字段的变化而不断的改变子查询的条件 example:
select tiele from movie old
where year <=any
(select year from movie where title = old.title)
该句用来查找是翻怕的电影(具有名字形同,但是拍摄年代更加早的电影)  相关子查询是很有用的查询方法,使用的好,可以查询很复杂的结果
是很实用的方法
接下来是集合操作符--集合操作符将2个查询结果组合成一个结果集,集合操作符有4个:union(联合),union all(联合所有),intersect(交集),minus(减集),使用集合操作符连接起来的select语句中的列遵循以下规则:
1)联合的结果必须具有相同的列数,并且对应列的数据类型必须相同
2)有long型的列不能使用
3)列标题来自第一个select 语句
下面一一介绍4个集合操作符:
1)union 返回2个查询选定的所有不重复的行 example:
select orderno from order_master
 union
select orderno form order_detail,这里你需要创建一个order_detail表与order_master有相同的orderno 列
2)union all 操作符 表示合并2个查询的所有行 example:
select orderno from order_master
 union all
select orderno form order_detail
3)INTERSECT 返回2个查询结果集的工作结果
select orderno from order_master
 intersect
select orderno form order_detail
4)minus 返回属于第一个结果集但是不属于第二的结果集的所有行,也就是在第一个结果集中排除在第二个结果集中出现的行example:
select orderno from order_master
 minus
select orderno form order_detail
对联合查询的结果可以进行排序 使用order by 字句,必须指定列索引来排序 而不是指定列名 列索引是一个丛1开始的正整数 example:
select qty_hand,max_level from itemfile
 union
select qty_ord,qty_deld from order_fetail order by 2
意思是根据 查询结果的第二列来进行排序。
这篇高级查询只是抛砖引玉,由于本人理解的也不是很深,难免存在很多错误的地方,希望大家多多留言指导。。。
另:oracle里面增加了很多的内置函数处理字段,这些函数功能很强大,我这里在网上找了oracle函数大全,每个函数都做了讲解,并配有例子,有需要的朋友可以留言我给你发过去。(本来想传上来的,但是不知道怎么发附件,汗!~~~)

 

 

 

 

 

原创粉丝点击