-多表查询

  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. react-native 在Xcode上传到iTunes Connect里报错

    在xcode里面点击“upload to app store”的时候,提示“the session's status is FAILED and the error description is 'C ...

  2. js 巧妙去除数组中的重复项

    1.代码如下: var toObject = function(a) { var o = {}; for (var i=0, j=a.length; i<j; i=i+1) { // 这里我调整 ...

  3. C++ 无名对象

    http://blog.sina.com.cn/s/blog_5f0e13360100bxlj.html 可以直接调用构造函数产生无名对象. 例如,下面的代码在函数fn()中,创建了一个无名对象: c ...

  4. JBOSS参数调优

        阅读目录 JBOSS参数调优 jvm调优讲解1 JVM调优讲解2 JVM常见配置汇总 JBOSS生产环境下JVM调优 JBOSS瘦身 JBoss性能优化:内存紧张的问题终于解决了(转载)--- ...

  5. MyEcplise的注册代码

    代码分析 package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputSt ...

  6. Kafka与MQ的区别

    作为消息队列来说,企业中选择mq的还是多数,因为像Rabbit,Rocket等mq中间件都属于很成熟的产品,性能一般但可靠性较强, 而kafka原本设计的初衷是日志统计分析,现在基于大数据的背景下也可 ...

  7. Ace admin 如何实现类似于freamset加载页面

    如上标题所述,ace admin做后台页面的时候,可以实现类似于用freamset的功能,但是ace admin做的比freamset更好,他可以用异步加载的形式展示,而加载的页面的内容可以尽可能的少 ...

  8. hdu 6169 gems gems gems【DP】

    题目链接:hdu 6169 gems gems gems Now there are n gems, each of which has its own value. Alice and Bob pl ...

  9. 第八章.Spring MVC

    基于MyEclipse开发 工程结构: 所使用到的jar: 代码: FruitControllerTest.java public class FruitControllerTest implemen ...

  10. 播放WAV文件和系统提示音