• Oracle(四)子查询


    --子查询  查询的结果集 被当作 另一个查询语句的表
    --01.查询招生部 所有男老师的姓名
    select  tname,dname from teacher,dept
    where teacher.deptno=dept.deptno
    and teacher.gendar='and dept.dname='招生部'

    --子查询实现
    select  tname,deptno from teacher
    where deptno=(select deptno from dept where dname='招生部')
    and gendar=''

    --02.查询老师姓名和对应的部门名称   使用子查询???! 
    --不使用别名 会默认把子查询语句作为列名
    select tname,(select dname from dept where deptno=teacher.deptno) as 部门
    from  teacher


    --03.查询在招生部以及人力部门的老师信息
    select * from teacher
    where deptno in
    (select deptno from dept where dname in('招生部','人力部'))

    --使用exists 代替in   使用not exists 代替 not in
    --exists 并不是返回一个结果集  返回true 或者false
    select * from teacher
    where  exists
    (select deptno from dept
    where
    deptno=teacher.deptno and
    dname in('招生部','人力部'))

    --  oracle中的链接操作符
    select  tname||'===='||sal||'==='deptno  from  teacher
    --事务控制语句

    **************=========创建表==========**********************

    --01.创建dept表
    create    table  dept(
    deptno number(2) primary key,
    dname   varchar2(20),
    loc   varchar2(20)
    );
    --02.插入数据
    insert  into  dept  values(10,'人力部','北京海淀');
    insert  into  dept  values(20,'财务部','北京海淀');
    insert  into  dept  values(30,'市场部','北京海淀');
    insert  into  dept  values(40,'技术部','北京海淀');
    --03.再次插入两条数据
    insert  into  dept  values(50,'市场部1','北京海淀');
    insert  into  dept  values(60,'技术部2','北京海淀');
    --设置回滚点
    savepoint  a;
    --再插入新数据
    insert  into  dept  values(70,'市场部3','北京海淀');
    --事务回滚到指定的回滚点
    rollback  to savepoint a;
    --04.查询dept表  有 50,60的部门
    select from dept;
    --05.回滚事务
    rollback;
    -- 查询dept表  有没有 50,60的部门
    select * from dept;

    --01. 子查询   查询招生部门所有的男老师姓名
    select tname,(select dname from dept where deptno=teacher.deptno) as 部门名称
    from teacher
    where deptno=(select deptno from dept where dname='招生部')
    and gendar=''



    --01. 使用内连接  01 查询招生部门所有的男老师姓名
    select tname,dname
    from teacher t,dept d
    where t.deptno=d.deptno
    and gendar='' and dname='招生部'

    --01. 使用内连接  02查询招生部门所有的男老师姓名
    select tname,dname
    from teacher t inner join dept d
    on t.deptno=d.deptno
    where gendar='' and dname='招生部'


    --02.自连接  查询老师的姓名 和导师的姓名
    select t1.tname 老师姓名,t2.tname 导师姓名 from teacher t1,teacher t2
    where t1.mgrno=t2.tno


    --03.使用左外链接查询老师的姓名,对应导师的姓名 以及部门
    select  t1.tname 老师姓名,t2.tname 导师姓名,d.dname 部门名称
    from  teacher t1 left join teacher t2
    on t1.mgrno=t2.tno
    left join  dept d
    on t1.deptno=d.deptno

    --使用内连接
    select  t1.tname 老师姓名,t2.tname 导师姓名,d.dname 部门名称
    from teacher t1,teacher t2,dept d
    where  t1.mgrno=t2.tno and t1.deptno=d.deptno


    --04.左外链接  以左表为准   右表中没有匹配的数据 返回空
    select * from
    teacher t1 left join dept d
    on t1.deptno=d.deptno

    --05.右外链接  以右表为准   左表中没有匹配的数据  不显示
    select * from
    teacher t1 right join dept d
    on t1.deptno=d.deptno

  • 相关阅读:
    MVP福利利用Azure虚拟机玩Windows Server 2012
    负载均衡的基本算法
    RavenDB:基于Windows/.NET平台的NoSQL数据库
    使用Autofac在ASP.NET Web API上实现依赖注入
    Mono 3 的默认Gc是Sgen
    MSDN 杂志 Windows 8 特刊
    AggSharp Agg的.NET 移植
    使用谷歌翻译/微软翻译迅速使你的博客支持多国语言
    Service Bus for Windows server
    用Xwt构建跨平台应用程序[转载]
  • 原文地址:https://www.cnblogs.com/xiaobaizhang/p/8645628.html
Copyright © 2020-2023  润新知