-多表查询

  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之多表查询的更多相关文章

  1. Oracle笔记 多表查询

    Oracle笔记  多表查询   本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: 3. 子查询的操作,并且结合限定查询.数据排序.多表查询.统计查 ...

  2. Oracle的多表查询

    多表查询概念: 所谓多表查询,又称表联合查询,即一条语句涉及到的表有多张,数据通过特定的连接进行联合显示. 基本语法: select column_name,.... from table1,tabl ...

  3. oracle SQL多表查询

    SQL多表查询 1.集合理论 1.1 什么是集合 具有某种特定性质的事物的总体. 集合的特性:无序性.互异性.确定性. 一个集合可以小到从一个表中取出一行中的一列.              1 ro ...

  4. Oracle查询优化-多表查询

    --合并结果集 --1.union all UNION ALL--单纯合并 ; --2.union UNION --将重复结果集合并 ; --------------使用命令窗口执行,查看union与 ...

  5. oracle数据库单表查询

    今天给大家分享的是关于数据库的单表查询,像单表查询/多表查询/分组查询/子查询,这些方法的使用在实际项目过程中会经常用到,作为一名合格的测试人员如果不会数据库那肯定是不行的,行走江湖可能随时会面临被侮 ...

  6. oracle习题-emp表查询练习

    emp表查询练习 1 查询emp表的全部记录 Select * from emp; 2 查询出每个雇员的编号.姓名.基本工资 Select empno,ename,sal from emp; 3 查询 ...

  7. 关于oracle数据库 跨表查询建立 视图的方法

    工作中很多时候都会遇到需要将两个不同的表空间甚至数据库的表进行联合查询或者建立视图的情况. 不同的表空间查询我们可以通过在将要查询的表前面加上 表空间的对应的用户名来实现,如有两个表空间分别对应两个用 ...

  8. Oracle数据库多表查询,子查询,集合运算

    记得自己要敲o~~~ select * from bonus; select * from salgrade; from dual; --笛卡尔积:两张表的乘积 select * from emp,d ...

  9. ORACLE关于锁表查询的部分SQL

    http://www.cnblogs.com/quanweiru/archive/2012/08/28/2660700.html --查询表空间名称和大小 SELECT UPPER (F.TABLES ...

随机推荐

  1. Word 2010 去除文字或段落背景色

    在复制网页文本到Word时,有时会带有网页上的背景颜色.下面采用两种方法解决这种问题,可根据不同需要进行选择. 方法一:清除样式 此种方法适用于只需要网页文字,而不想要网页任何样式信息,如字体大小,段 ...

  2. C#中的多线程 - 并行编程 z

    原文:http://www.albahari.com/threading/part5.aspx 专题:C#中的多线程 1并行编程Permalink 在这一部分,我们讨论 Framework 4.0 加 ...

  3. July 22nd 2017 Week 29th Saturday

    If you are not brave enough, no one will back you up. 如果你不够勇敢,没人会替你坚强. I was told that the real man ...

  4. OC基础数据类型-NSArray

    1.数组的初始化 NSArray *array = [[NSArray alloc] initWithObjects:@"One", @"Two", @&quo ...

  5. Oracle拆分字符串,字符串分割的函数。

    第一种:oracle字符串分割和提取 分割 create or replace function Get_StrArrayLength ( av_str varchar2, --要分割的字符串 av_ ...

  6. 可变对象(immutable)和不可变对象(mutable)

    可变对象(immutable)和不可变对象(mutable) 这个是之前一直忽略的一个知识点,比方说说起String为什么是一个不可变对象,只知道因为它是被final修饰的所以不可变,而没有抓住不可变 ...

  7. codeforces 848B Rooter's Song

    题目链接 正解:排序+模拟. 我们注意到两个点碰撞的必要条件,$pi+tj=pj+ti$,移项以后发现就是$pi-ti=pj-tj$,那么我们可以把$p-t$相同的点分为同一组. 然后我们还可以发现一 ...

  8. JAVA JAVA面试题和项目面试核心要点精华总结(想进大公司必看)

    http://blog.csdn.net/ourpush/article/details/53706524 1.常问数据库查询.修改(SQL查询包含筛选查询.聚合查询和链接查询和优化问题,手写SQL语 ...

  9. __call、__set 和 __get的用法

    1. __call的用法 PHP5 的对象新增了一个专用方法 __call(),这个方法用来监视一个对象中的其它方法.如果你试着调用一个对象中不存在的方法,__call 方法将会被自动调用. 例:__ ...

  10. l1 l2 loss

    衡量预测值与真实值的偏差程度的最常见的loss: 误差的L1范数和L2范数 因为L1范数在误差接近0的时候不平滑,所以比较少用到这个范数 L2范数的缺点是当存在离群点(outliers)的时候,这些点 ...