• Oracle 学习笔记(二)


    Oracle 学习笔记

    Day_2

    image-20201225155308858

    • 多表查询

    • image-20201226144913604

    已有表

    image-20201225160349747

    已有的表格:

    select * from bonus;
    
    /*
    多表查询:
    笛卡尔积:实际上是两张表的乘积
    */
     
    select * from emp,dept;
    
    select * from emp e1,dept e2 where e1.deptno == d1.deptno;
    
    --查询员工编号,员工姓名,经理的编号,经理的姓名
     select * from em 
     
    --查询员工编号,员工姓名,员工的部门名称,经理的编号,经理的姓名
    select e1.empno,e1.ename, d1.dname , e1.mgr , m1.ename 
     from emp e1,emp m1,dept d1 where e1.mgr= m1.empno and e1.deptno = d1. deptno;
    
    
     /*
     内联接:
    险式内联接:
    等值内联接:where e1.deptno = d?. deptno ;不等值内联接:where ei.deptno <>di.deptno;自联接:自己连接自己
    内联接:
    select *from表iinner join 表2 in 连接条件I
    
    
    外连接:
    左外连接 ;left outer join 左表的所有记录,如果所有记录没有表记录, 就显示为空。
    
    右外连接“: right outer join 
    
    outer 关键字可以忽略
    
    Oracle的外连接(+): 如果没有对应的记录,就显示空值
    
    select * from emp e1,dept d1 where e1.deptno = d1.deptno(+)
    
    */
     
     --查询员工姓名 和员工 部门所属的位置
     select el.ename,d1.loc from emp e1 ,dept d1 where e1.deptno == d1.deptno ;
      
    

    image-20201226143848316

    --
    
    • 子查询

      --查询员工中的最高工资
      
      select max(sal) from emp ; --最高工资
      
      select * from emp where sal = (select max(sal) from emp )
      
      /*子查询语: 嵌套查询
      分类别:
      单行子查询:  是使用关系 查询语句 < > + =  等
      多行子查询: */
      
      --查询出比给雇员7654 的工资高,同时和7788 从事 相同工作的员工信息。
      
      /*分析:
      1.查询7654 的工资
      
      2.7788 从事的工作 
      
      3.符合条件的员工信息*/
      
      select sal from emp where empno = 7654;
      
      select job from emp where empno = 7788;
      
      select * from emp where  sal > (select sal from emp where empno = 7654) and job = (select job from emp where empno = 7788);
      

      sal:

      image-20201226150414163

      符合条件”

    image-20201226150833026

    --查询领导信息
    --1.查询所有经理的编号
    select mgr from emp;
    select dissinct mgr from emp;
    --2.结果
    select * from emp where empno in (select mgr from emp);
    
    --查询不是领导的信息
    
    select * from emp where empno not in (select mgr from emp) --写法错误,对NULL 值的理解错误
    
    
    select * from emp where empno <>all (select mgr from emp)--方法一:<> all 非所有的 sql 里的非
    
    select * from emp where empno not in (select mgr from emp where mgr is not NULL) --非 领导且不为空值中查找 
    
    /*
    内联接,单行子查询,多行子查询
    in
    not in
    any
    all
    exists
    通常情况下,数据库中不要出现null not null
    
    */
    
    /*
    --查词每个部门最低工资的员工信怎和他所在的部门信息
    --1.知谐每个部门7的最低工资
    --2.员工工资等宁脑所郊部i门了的最低工资
    --查询部门的相关信息  
    */
    
    select deptno ,min(sal) minsal  from emp group by deptno'
    
    select * from emp e1,dept d1 where e.deptno = d1.pno
    
    
    /*
    exists(存在):存在的意,思
        当作布尔值来处理:
        当查询语句有结果的时候,就是返回true
        否则返回的是false
        数据最比较大的时候是非常高效的
     */
    
    
    select * from emp order by sal desc
    /*
    按照列名是一个排序顺序,可以是:
    ASC表示按升序排序
    DESC表示按降序排序
    */
    select rownum,e1.* from emp e1 order by sal desc 
    
    /*
    rownum : 伪列,系统自动生成的一列,用:来表示行号
    rownum是oracle中特有的用来表示行号的,默认值/起始值是1,在查询出结果之后,再添加1
    */
    
    
    --统计每年入职的员工个数
    
    select hiredate from emp;
    
    select to_char (hiredate,'yyyy') from emp ;
    
    select to_char (hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy');
      
    

    image-20201226190216036

    /*
    rowid :伪列每行记录所存放的真实物理地址
    rownum : 行号,每查询出记录之后,就会添加一个行号
    */
    select * rowid ,e.* from emp e;
    
    create table p(
    name varchar2(10)
    )
    insert into p value('黄伟福');
    insert into p value('黄三');
    insert into p value('代五');
    
    select rowid,* from p;
    
    delete from p p1 where rowid > (select min(rowid) from p p2 where p1.name = p2.name );
    
    
    

    image-20201226191434471

    • Oracle 分页查询

    (1)>= y,<= x表示从第y行(起始行)~x行(结束行) 。
    (2)rownum只能比较小于,不能比较大于,因为rownum是先查询后排序的,例如你的条件为rownum>1,当查询到第一条数据,rownum为1,则不符合条件。第2、3…类似,一直不符合条件,所以一直没有返回结果。所以查询的时候需要设置别名,然后查询完成之后再通过调用别名进行大于的判断。

    --查询第6-10 的记录
    
    select rownum,emp.* from emp;
    select rownum hanghao,emp.* from emp;
    select * from (select rownum hanghao ,emp.* from emp) where tt.hanghao between 6 and 10;
    /*
    (select rownum hanghao ,emp.* from emp) 行号,emp的成员元素
     删选条件: 行号 在 6 到 10 之间
    */
    

    image-20201226192224705

    • 集合运算
    /*
    集合运算:
    并集:将两个查询结果进行合并交集 
    union / union all
    差集: 
    */
    

    效果图

    image-20201226192851216

    image-20201226193118526

    /*
    集合运算中的注意事项:
    1.列的类型要一致
    2.按照顺序写
    3.列的数量要一致,如果不足,用空值填充,如果没有,应该用null 填充 。
    */
    
  • 相关阅读:
    Singleton 单例模式 泛型 窗体控制
    SpringMVC异常处理注解@ExceptionHandler
    如何实现beanutils.copyProperties不复制某些字段
    Spring 注解学习笔记
    使用客户端SVN在Update时遇到Previous operation has not finished; run 'cleanup' if it was interrupted,需要cheanup文件夹,解决方式
    jQuery的选择器中的通配符
    mysql str_to_date字符串转换为日期
    Mybatis中mapper.xml文件判断语句中的单双引号问题
    Jquery中each的三种遍历方法
    Javascript 中动态添加 对象属性
  • 原文地址:https://www.cnblogs.com/HelloBytes/p/14193989.html
Copyright © 2020-2023  润新知