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表中查询出学生的学号,
     然后合并查询结果 UNIONunion 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;

猜你喜欢

转载自blog.csdn.net/qq_41832689/article/details/81409267