oracle数据库的高级查询方法 多表联合查询

  oracle查询sql

    一,简单查询oracle

      order by 1,2spa

        select t.sno, t.sname, t.ssex from STUDENT t order by 1,2,3code

      group by 加强版it

        SELECT p.toma, p.ptype, SUM(p.lastcou) FROM product p GROUP BY rollup(p.toma, p.ptype)io

    二,高级查询(多表链接查询)ast

      笛卡尔积的概念:扩展

        所谓笛卡尔积,通俗点说就是指包含两个集合中任意取出两个元素构成的组合的集合。假设R中有元组M个,S中有元组N个,则R和S的笛卡select

        尔积中包含的元组数量就是M*N。这个规则能够向多个关系扩展。rollup

      分类:

        内链接:select s.sname, r.cno,r.degree from student s, score r where s.sno=r.sno

        等值链接------用=的链接

        不等值链接

          外链接(啥也不写就是内链接, 直接写两个表名)---左外链接, 右外链接

            select * from student s left outer join course c on s.sno=c.cno

            (+)修饰符的使用

              select s.sname, r.cno,r.degree from student s, score r where s.sno=r.sno(+)----等同于外链接的结果

            数据字典的使用

          自链接(本身骗一下本身):  select * from z_course c1, z_course c2 where c1.cour_code=c2.p_cour_code 

          层次查询
            CONNECT BY PRIOR 链接条件 START WITH 开始条件

            select * from Z_COURSE t connect by prior t.cour_code=t.p_cour_code start with t.cour_name like '%国际%'

            伪列:

              level: 加上一列显示级别

              select t.*, level from Z_COURSE t connect by prior t.cour_code=t.p_cour_code start with t.cour_name like '%国际%'

              rownum:加上一列行号

                select s.*, rownum from student s 

                使用rownum分页

                  select * from (select s.*, rownum rn from student s where rownum<=10) t where t.rn>5

          子查询(sql语句的执行顺序)

            单行子查询: 能够用=号

            多行子查询:不能够用=号,能够用 in

            exists关键字的使用: 是否存在

              select * from student t where exists(select 1 from student t1 where t1.sno=105)

相关文章
相关标签/搜索