数据库——多表查询

多表查询
  笛卡儿积:
    从第一张表中选出第一条记录,和(and)第二个表的所有记录进行组合;
    从第一张表去第二条记录,和第二张表中的所有记录组合;
    不加过滤结果;
    select EMP.ename, EMP.sal, DEPT.dname from EMP, DEPT where EMP.deptno = DEPT.deptno;
    显示部门号为10的部门名,员工名和工资
    select ename, sal,dname from EMP, DEPT where EMP.deptno=DEPT.deptno and DEPT.deptno = 10;
    显示各个员工的姓名,工资,及工资级别
    select ename, sal, grade from EMP, SALGRADE where EMP.sal between losal and hisal;
  自连接:同一张表连接查询   
    显示员工FORD的上级领导的编号和姓名 
    子查询:
    select empno,ename from emp where emp.empno=(select mgr from emp where ename='FORD');
    多表查询:(自查询)--笛卡儿积(M*N)
    select leader.empno,leader.ename from emp leader, emp worker
    where leader.empno = worker.mgr and worker.ename='FORD';
  子查询:指嵌入在其他sql语句中select语句,(嵌套语句)
    单行子查询:
     显示SMITH同一部门的员工
     select * from EMP WHERE deptno = (select deptno from EMP where ename='smith');
    多行子查询:(返回单列)
     in关键字;查询和10号部门的工作相同的雇员的名字,岗位,工资,部门号,但是不包含10自己的
         select * from EMP where job in ( 
         select distinct job from EMP where deptno=10
         ) and deptno !=10;
     all关键字;(sal (单个)比all(集合中所有的工资)高)
      显示工资比部门30的所有员工的工资高的员工的姓名、工资和部门号
         select ename, sal, deptno from EMP 
         where sal > all(select sal from EMP where deptno=30);
     any关键字;(sal (单个)比any(集合中任意一个)高)
      显示工资比部门30的任意员工的工资高的员工的姓名、工资和部门号
         select ename, sal, deptno from EMP 
         where sal > any(select sal from EMP where deptno=30);
    多列子查询:(返回多个子查询)
      查询和SMITH的部门和岗位完全相同的所有雇员,不含SMITH本人
         select ename from EMP 
         where (deptno, job)=(select deptno, job from EMP where ename='SMITH') 
         and ename != 'SMITH'; 
    from 子句中使用子查询:子查询语句在from子句中
      显示高于自己部门平均工资的员工的信息
         select ename, deptno, sal, asal from EMP,   
        (select avg(sal) asal, deptno dt from EMP group by deptno) as tmp    
         where EMP.sal > tmp.asal and EMP.deptno=tmp.dt;
      查找每个部门工资高的人的详细资料
         select EMP.ename, EMP.sal, EMP.deptno, ms from EMP,
        (select max(sal) ms, deptno from EMP group by deptno) tmp 
         where EMP.deptno=tmp.deptno and EMP.sal=tmp.ms;
      显示每个部门的信息(部门名,编号,地址)和人员数量 
         1.多表
         select dname, DEPT.deptno, loc,count(*) '部门人数' from EMP, DEPT  
         where EMP.deptno=DEPT.deptno
         group by DEPT.deptno;
         2.使用子查询
         select dept.deptno,dept.dname,dept.loc from dept,
         ( select deptno ,count(ename) from emp group by deptno)--(每个部门的人员数量 )
         as demp 
         where demp.deptno=dept.deptno; 
    合并查询:
       union(操作符):取得两个结果的并集,自动去重(来自多张表查询时,需要联合查询)
         将工资大于25000和职位是MANAGER的人找出来
         select empno,ename,sal,job from emp 
         where sal>25000 or job ='MANAGER';--普通语句         
         select ename, sal, job from EMP where sal>2500 union  
         select ename, sal, job from EMP where job='MANAGER';--去掉了重复记录 
       union all(操作符):取得两个结果的并集,不去重
         select ename, sal, job from EMP where sal>2500 union all 
         select ename, sal, job from EMP where job='MANAGER'; 
内连接和外连接:
     内连接:(笛卡儿积)
         select ename, dname from EMP, DEPT
         where EMP.deptno=DEPT.deptno and ename='SMITH'; 
       select 字段 from 表1 inner join 表2 on 连接条件 and 其他条件;
         select ename, dname from EMP inner join DEPT
         on EMP.deptno=DEPT.deptno and ename='SMITH';
     外连接:
       左外连接:左侧的表完全显示;
         select 字段名  from 表名1 left join 表名2 on 连接条件;
         select * from stu left join exam on stu.id=exam.id; 
       右外连接: 右侧的表完全显示;
         select 字段 from 表名1 right join 表名2  on  连接条件;
         select * from stu right join exam on stu.id=exam.id;
    

猜你喜欢

转载自blog.csdn.net/fayfayfaydyt/article/details/82082515