zoukankan      html  css  js  c++  java
  • sql语句回忆录1-多表连接子查询

    --多表连接查询,在多张表中查询符合条件的记录
    --注意:给表起简短的别名
    select * from emp e
    --给字段起别名:字段名 别名,当别名为数字(包括数字开头)或者有空格时需要用双引号括起来
    select sysdate "2017" from dual

    --内连接也称为等值连接
    --语法:表1 inner join 表2 on 条件,同时inner可以省略
    --查询工资大于2000元的员工的姓名,工资和部门名称,按照工资降序排列
    --分析:工资和姓名在emp表,部门名称在部门表,涉及到两张表
    select e.ename,e.sal,d.dname
    from emp e,dept d where e.sal>2000 and e.deptno = d.deptno order by e.sal desc

    select e.ename,e.sal,d.dname
    from emp e inner join dept d on e.deptno = d.deptno where e.sal>2000 order by e.sal desc

    --查询每个学生每门课的成绩,显示学生姓名,课程名,成绩
    select st.stuname,sc.scores,co.cname
    from student st,score sc,couse co where st.stuid = sc.stuid and sc.cid = co.cid

    --右连接,查询已右表为基础,右表中的记录全部显示,左表只显示符合条件的记录,不符合条件的以null填充
    --语法:表1 right join 表2 on 条件
    --语法2:表1,表2 where 表1.字段(+)= 表2.字段
    --查询每个部门的员工姓名,工资,部门名
    select e.ename,e.sal,d.dname
    from emp e right join dept d on e.deptno = d.deptno
    --查询工资大于2000元的员工的姓名,工资和部门名称,按照工资降序排列(右连接)
    select e.ename,e.sal,d.dname
    from emp e right join dept d on e.deptno = d.deptno where e.sal>2000 order by e.sal desc

    select e.ename,e.sal,d.dname
    from emp e,dept d where e.deptno (+)= d.deptno and e.sal>2000 order by e.sal desc

    --左连接,以左表为基础,左表的记录全显示,右表只显示符合条件的记录,不符合的以null填充
    --语法:表1 left join 表2 on 条件
    --语法:from 表1,表2 where 表1.字段 = 表2.字段(+)
    --查询工资大于2000元的员工的姓名,工资和部门名称,按照工资降序排列(右连接)
    select e.ename,e.sal,d.dname
    from dept d left join emp e on d.deptno = e.deptno where e.sal>2000 order by e.sal desc

    --注意:所谓的左右连接,一般都是主键表为基准的,主键表在左边就是左外连接,主键表在右边就是右外连接

    --全连接
    --语法:表1 full join 表2 on 条件
    select * from emp e full join dept d on e.deptno = d.deptno
    --自然连接:两张表中必须有相同,即列名相同,列的数据类型相同,oracle自动根据相同的列来查询,不需要条件
    select * from emp e natural join dept d

    --子查询
    --在select update delete语句中又包含了select,即内部的查询结果作为外部的查询条件来使用。
    --查询出我们的销售部SALES下面的员工的姓名,工资
    --分析:select deptno from dept where dname = ''SALES --查询出销售部的部门编号
    select ename,sal from emp where deptno=(select deptno from dept where dname = 'SALES')
    --单行子查询:子查询的结果为一个值,需要用到运算符 = ,!= ,<>,< ,>,>=,<=
    --查询出平均工资高于部门编号为30的最高工资的部门信息
    --分析:求出部门号为30的员工的最高工资
    select e.deptno,avg(e.sal) from emp e
    group by e.deptno
    having avg(e.sal)>(select max(sal) from emp where deptno=30)

    --查询出工资比本部门平均工资高的员工的姓名,工资,部门号
    select e1.ename,e1.sal,e1.deptno from emp e1
    where e1.sal>(select avg(e2.sal) from emp e2 where e1.deptno = e2.deptno)

    --自连接:给一张表起两个别名,相当于在两张表中查询数据
    --查询所有员工的姓名即上级的姓名
    select e1.ename 员工姓名,e2.ename 经理姓名
    from emp e1,emp e2 where e1.mgb = e2.empno

    --多行子查询:子查询的结果为多个值,需要用的运算符in all any
    --any 与子查询结果中的任意一个相比,只要符合条件,就被查询出来
    --查询工资低于任意一个销售SALES员的工资的员工信息
    select ename,sal,job from emp
    where sal<any(select sal from emp where job='SALESMAN')

    select ename,sal,job from emp where sal<any(1600,1500,1250)

    --in子查询,只要在in后面的数值列表里就会被查出来
    select * from emp where empno in(7699,7566,7902)
    --查询部门号大于10的员工的姓名,工作,部门号
    select ename,job,deptno from emp where deptno in(select distinct deptno from emp where deptno>10)
    --批量删除的时候,把员工编号是7902 7566 7998 9999 8888
    delete from emp where deptno in(7902,7566,7998,9999,8888)
    delete from emp where deptno not in(7902,7566,7998,9999,8888)

    --在子查询中如果聚合函数作为一列必须起别名

  • 相关阅读:
    MyBatis:分页的实现
    Mybatis之配置文件
    Java之创建线程的方式四:使用线程池
    Java之创建线程的方式三:实现Callable接口
    Java之线程通信的应用:经典例题:生产者/消费者问题
    Java之线程通信的方法
    Java之解决线程安全问题的方式三:Lock锁
    Java之同步方法处理实现Runnable接口的线程安全问题
    Java之同步方法处理继承Thread类的线程安全问题
    01 while 循环输入1 2 3 4 5 6 8 9 10
  • 原文地址:https://www.cnblogs.com/Jack-Imane/p/6517735.html
Copyright © 2011-2022 走看看