Oracle之多表查询
-多表查询
1.交叉连接
select * from t_class for update;
select * from t_student for update;
select for update 是为了在查询时,避免其他用户以该表进行插入,修改或删除等操作,造成表的不一致性.
查询学生信息及对应的班级信息
select t1.*,t2.*
from t_student t1,t_class t2
--交叉连接获取的结果是一个笛卡尔乘积
--也就是表1中的数据都要和表2中的每条数据连接一次 是数据条数时两个表数据量的乘积
2.等值连接
select t1.*,t2.* -- 100000 2(两者相乘)
from t_student t1,t_class t2 -- 10000 100 100W 获取的结果集可能非常大 效率很低
where t1.classid = t2.cid -- 10条
3.内连接
select t1.*,t2.*
from t_student t1 inner join t_class t2 on t1.classid = t2.cid
select t1.*,t2.*
from t_class t2 inner join t_student t1 on t1.classid = t2.cid
--先比较过滤,再保存到内存中 左表的数据一条一条的和右表的数据进行连接比较
-- 左边的数据和右边的数据满足 on 关键字后面的条件保留
--在连接的时候一般将数据量小的表放在连接符合的左侧
--但不满足条件就会过滤掉,有时会出错
查询出学生表中的所有的学生信息及对应的班级信息
4.左连接:在内连接的基础上保留左侧不满足条件的数据
左表是主表,不满足条件也会保留
select t1.*,t2.*
from t_student t1 left outer join t_class t2
on t1.classid = t2.cid
select t2.*,t1.*
from t_class t1 left join t_student t2
on t1.cid = t2.classid
5.右连接:在内连接的基础上保留右侧不满足条件的数据
select t1.*,t2.*
from t_student t1 right join t_class t2
on t1.classid = t2.cid
6.全连接:在内连接的基础上保留左右两侧不满足条件的数据
全连接是左连接和右连接的并集
select t1.*,t2.*
from t_student t1 full join t_class t2
on t1.classid = t2.cid
例题:
--drop table student;
create table student (
id number(3) PRIMARY key,
name VARCHAR2(20) not null,
sex varchar2(4),
birth number(4),
department varchar2(20),
address VARCHAR2(50)) --创建score表。SQL代码如下:
create table score(
id number(3) PRIMARY key,
stu_id number(3) not null,
c_name VARCHAR(20) ,
grade number(3)
) -- 向student表插入记录的INSERT语句如下:
insert into student values(901,'张老大','男',1985,'计算机系','北京市海淀区');
insert into student values(902,'张老二','男',1986,'中文系','北京市昌平区');
insert into student values(903,'张三','女',1990,'中文系','湖南省永州市');
insert into student values(904,'李四','男',1990,'英语系','辽宁省阜新市');
insert into student values(905,'王五','女',1991,'英语系','福建省厦门市');
insert into student values(906,'王六','男',1988,'计算机系','湖南省衡阳市');
-- 向score表插入记录的INSERT语句如下:
insert into score values(1,901,'计算机',98);
insert into score values(2,901,'英语',80);
insert into score values(3,902,'计算机',65);
insert into score values(4,902,'中文',88);
insert into score values(5,903,'中文',95);
insert into score values(6,904,'计算机',70);
insert into score values(7,904,'英语',92);
insert into score values(8,905,'英语',94);
insert into score values(9,906,'计算机',90);
insert into score values(10,906,'英语',85); SELECT * from student;
select * from score; --1、查询student表的第2条到4条记录
select s2.*,rownum
from (select s.*,rownum num from student s where rownum <=4) s2
where s2.num >=2; --2、从student表查询所有学生的学号(id)、
姓名(name)和院系(department)的信息 select id 学号,name 姓名,department 院系
from student; --3、从student表中查询计算机系和英语系的学生的信息
select *
from student
where department='计算机系' or department = '英语系'; --4、从student表中查询年龄25~30岁的学生信息
select *
from student
where (extract(YEAR from sysdate)-birth) between 25 and 30; --5、从student表中查询每个院系有多少人
select department,count(1)
from student
group by department; --6、从score表中查询每个科目的最高分
select c_name,max(grade)
from score
group by c_name; --7、查询李四的考试科目(c_name)和考试成绩(grade)
注意: '=' 只有在确定结果是一个的情况下使用,不确定的使用用 'in'
select s.c_name,s.grade
from score s
where stu_id = (select id from student where name='李四');
--in 的效率会比较低 select s.c_name,s.grade
from score s
where exists (select id from student s2 where name='李四' and s2.id = s.stu_id); select t1.* ,t2.* --内连接
from student t1 inner join score t2
on t1.id = t2.stu_id and t1.name='李四'; select t1.* ,t2.* --效率比较高
from (select * from student where name='李四') t1 inner join score t2
on t1.id = t2.stu_id;
--8、用内连接的方式查询所有学生的信息和考试信息
select s.*,s2.*
from student s inner join score s2 on s.id = s2.stu_id; --9、计算每个学生的总成绩
select stu_id,sum(grade)
from (select stu_id,grade from score s1)
group by stu_id; --10、计算每个考试科目的平均成绩
select c_name,avg(grade)
from score
group by c_name --11、查询计算机成绩低于95的学生信息
select * from student
where id in (
select stu_id
from score s
where s.c_name ='计算机' and s.grade < 95) select s.* ,s2.* --内连接
from student s inner join score s2
on s2.c_name ='计算机' and s2.grade < 95 and s2.stu_id = s.id select s.* ,s2.* --比之前的内连接效率要高,先查出计算机低于95的信息减少数据条数
from (select * from score where c_name ='计算机' and grade < 95) s2 inner join student s
on s2.stu_id = s.id --12、查询同时参加计算机和英语考试的学生的信息
select * from student
where id in(--或者用exists
select stu_id
from score
where c_name='英语' and stu_id in(select stu_id
from score s1
where c_name='计算机') ) select * from student s
where exists(--exists
select stu_id
from score s2
where c_name='英语' and stu_id in(select stu_id
from score s1
where c_name='计算机')
and s.id = s2.stu_id
) select * --完全的内连接
from (select s1.stu_id
from (select * from score where c_name='英语') s2 inner join
(select * from score where c_name='计算机') s1
on s1.stu_id = s2.stu_id
) s3 inner join student s4 on s4.id = s3.stu_id; select *
from student --内连接配合子查询
where id in (select s1.stu_id
from (select * from score where c_name='英语') s2 inner join
(select * from score where c_name='计算机') s1
on s1.stu_id = s2.stu_id
) --13、将计算机考试成绩按从高到低进行排序
select *
from score
where c_name='计算机'
order by grade desc --14、从student表和score表中查询出学生的学号,
然后合并查询结果 UNION与union all select stu_id
from score
union
select id
from student --15、查询姓张或者姓王的同学的姓名、院系和考试科目及成绩 select s.name,s.department,s2.c_name,s2.grade
from (select id,name,department from student
where name like '张%' or name like '王%') s inner join score s2
on s.id = s2.stu_id; --16、查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩 select s.name,s.age,s.department,s.address,s2.c_name,s2.grade
from (select id,name,extract(YEAR from sysdate)-birth age,department,address from student
where address like '湖南%') s inner join score s2
on s.id = s2.stu_id;
Oracle之多表查询的更多相关文章
- Oracle笔记 多表查询
Oracle笔记 多表查询 本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: 3. 子查询的操作,并且结合限定查询.数据排序.多表查询.统计查 ...
- Oracle的多表查询
多表查询概念: 所谓多表查询,又称表联合查询,即一条语句涉及到的表有多张,数据通过特定的连接进行联合显示. 基本语法: select column_name,.... from table1,tabl ...
- oracle SQL多表查询
SQL多表查询 1.集合理论 1.1 什么是集合 具有某种特定性质的事物的总体. 集合的特性:无序性.互异性.确定性. 一个集合可以小到从一个表中取出一行中的一列. 1 ro ...
- Oracle查询优化-多表查询
--合并结果集 --1.union all UNION ALL--单纯合并 ; --2.union UNION --将重复结果集合并 ; --------------使用命令窗口执行,查看union与 ...
- oracle数据库单表查询
今天给大家分享的是关于数据库的单表查询,像单表查询/多表查询/分组查询/子查询,这些方法的使用在实际项目过程中会经常用到,作为一名合格的测试人员如果不会数据库那肯定是不行的,行走江湖可能随时会面临被侮 ...
- oracle习题-emp表查询练习
emp表查询练习 1 查询emp表的全部记录 Select * from emp; 2 查询出每个雇员的编号.姓名.基本工资 Select empno,ename,sal from emp; 3 查询 ...
- 关于oracle数据库 跨表查询建立 视图的方法
工作中很多时候都会遇到需要将两个不同的表空间甚至数据库的表进行联合查询或者建立视图的情况. 不同的表空间查询我们可以通过在将要查询的表前面加上 表空间的对应的用户名来实现,如有两个表空间分别对应两个用 ...
- Oracle数据库多表查询,子查询,集合运算
记得自己要敲o~~~ select * from bonus; select * from salgrade; from dual; --笛卡尔积:两张表的乘积 select * from emp,d ...
- ORACLE关于锁表查询的部分SQL
http://www.cnblogs.com/quanweiru/archive/2012/08/28/2660700.html --查询表空间名称和大小 SELECT UPPER (F.TABLES ...
随机推荐
- 微软与Node.js的开源之旅
微软近年来在开源领域可谓是大刀阔斧的前进中,继2015年微软与红帽合作,微软智能云Azure与Linux进一步融合等举措之后,2016年,微软继续加大开源之举,大力推进Node.js的开发和开源社区的 ...
- window 服务注册、卸载
1.以管理员身份打开 命令窗口 2.服务注册命令:sc create myServer binpath= path 3.服务卸载命令:sc delete myServer Topshelf 服务安装 ...
- JS 获取指定日期的前几天,后几天
function getNextDate(date,day) { var dd = new Date(date); dd.setDate(dd.getDate() + day); var y = dd ...
- vagrant安装centos7
1. 安装VirtualBox 去官网https://www.virtualbox.org/wiki/Downloads下载最新版的Virtualbox,然后双击安装,一直点击确认完成. 2. 安装V ...
- WebExtensions小例
一:简述 扩展是修改Web浏览器功能的代码位.它们使用标准的Web技术(JavaScript,HTML和CSS)以及一些专用的JavaScript API编写.其中,扩展程序可以向浏览器添加新功能或更 ...
- kill -9 和kill-15的区别
kill -9大家应该是非常熟悉的,杀死进程一般用kill -9的吧. 今天接触到kill -15,kill -15也是杀死进程的.那个kill -15和kill -9有什么区别呢? 其实kill - ...
- System IPC 与Posix IPC(共享内存)
系统v(共享内存) 1.对于系统V共享内存,主要有以下几个API:shmget().shmat().shmdt()及shmctl(). 2.shmget()用来获得共享内存区域的ID,如果不存在指定的 ...
- vbox安装 ubuntu server 后 安装增强包
用vbox安装虚拟机系统如果不装增强包, 有很多东西就有点不好用-用vbox安装ubuntu server时,点击菜单中的安装增强功能.因为ubuntu server版本没有ui,所以不能很方便滴找到 ...
- 正则工具类 -- RegexUtils
import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util. ...
- select、poll和epoll比较
select select能监控的描述符个数由内核中的FD_SETSIZE限制,仅为1024,这也是select最大的缺点,因为现在的服务器并发量远远不止1024.即使能重新编译内核改变FD_SETS ...