zoukankan      html  css  js  c++  java
  • SQL 表连接

    一、表连接【重难点】
        查询语句中from后面的表超过2个,这种连接称为表连接查询
        分类:内连接、外连接、自连接、交叉连接
        语法:  select *
                from 表1 join 表2
                on 连接条件
                where...group by..having..order by...
        1、内连接 [inner] join   【重点】
            --查询员工的信息(编号、姓名、工资、部门编号、部门名称)
            select employee_id,last_name,salary,employees.department_id,DEPARTMENT_NAME  
            from employees join departments
            on employees.department_id = departments.department_id
            
            select employee_id,last_name,salary,e.department_id,DEPARTMENT_NAME  
            from employees e join departments d
            on e.department_id = d.department_id
            
            内连接特点:1)必须要有连接条件
                        2)内连接结果:符合连接条件的数据
                        3)2个表没顺序要求
        2、外连接 (左外连接 left [outer] join/右外连接 right [outer] join/全部外连接 full [outer] join)
            --查询所有员工的信息(编号、姓名、工资、部门编号、部门名称)
            select *
            from employees e left join departments d
            on e.department_id = d.department_id
            
            左外连接的特点:1)必须要有连接条件
                            2)左外结果:符合条件的数据+左表中不符合条件的数据
                            3)2个表有顺序要求,以左表为主,右表为辅
                            4)左表中不符合条件的数据(左表有,但右表没有),如果要显示右表中字段的值,填充为null
            select *
            from departments d right join employees e
            on e.department_id = d.department_id
            右外连接的特点:1)必须要有连接条件
                            2)右外结果:符合条件的数据+右表中不符合条件的数据
                            3)2个表有顺序要求,以右表为主,左表为辅
                            4)右表中不符合条件的数据(右表有,但左表没有),如果要显示左表中字段的值,填充为null
            
            --查询没有员工的部门(编号、名称)
            select *
            from departments d  left join employees e
            on e.department_id = d.department_id
            where e.employee_id is null
            
            --查询所有部门的员工情况和所有员工的部门情况(符合条件的106+部门中不符合条件16+员工中不符合条件1)
            select *
            from departments d  full join employees e
            on e.department_id = d.department_id
            
            完全外部连接的特点:1)必须要有连接条件
                                2)全部外的结果:符合条件的数+左表中不符合+右表中不符合的
                                3)2个表没有顺序要求
                                4)哪个表中不符合条件,没数据对应填充null
        3、交叉连接 cross join 【了解】
            笛卡尔积  5    8  --》 40
            select *
            from departments d  cross join employees e
            
            select employee_id,last_name,salary,e.department_id,DEPARTMENT_NAME
            from departments d, employees  e
            where e.department_id = d.department_id
            
        --查询各部门具有本部门最高工资的员工
        --1)查询各部门的最高工资
            select department_id, max(salary) as maxSalary
            from employees
            group by department_id        -->tb1
        --2)查各部门具有本部门最高工资的员工
            select *
            from employees e join tb1
            on e.department_id = tb1.department_id
            where e.salary = tb1.maxSalary
        --3)合成
            select *
            from employees e join (select department_id, max(salary) as maxSalary
            from employees
            group by department_id) tb1
            on e.department_id = tb1.department_id
            where e.salary = tb1.maxSalary
        4、自连接 特殊内连接,参与连接的表是同一个表 [inner] join
        --查询员工的编号、姓名、工资、经理ID、经理姓名
            select e1.employee_id , e1.last_name, e1.salary, e1.manager_id, e2.last_name
            from employees e1 join employees e2
            on e1.manager_id = e2.employee_id
        5、多表连接
        --查询员工的编号、姓名、部门id、部门名称、所在地名称
            select e.employee_id,e.last_name,e.department_id,d.department_name, l.CITY  
            from employees e join departments d
            on e.department_id = d.department_id
            join locations l
            on d.location_id = l.location_id
        
        语法:select *
                from 表1 join 表2
                on 连接条件
                join 表3
                on 连接条件
                where............
        
        -----------
        SQL的分类:
            DQL: Data Query Language 数据查询语言 select【重点】
            DDL: Data Define Language 数据定义语言 建表、改表、删表、建视图/序列
            DML: Data Manipulate Language 数据操纵语言    对数据的添、修改、删除【重点】
            DCL: Data Control Language 数据控制语言 分配权限
            TCL: Transaction Control Language 事务控制语言 事务的提交、回滚

  • 相关阅读:
    一个java程序员自学IOS开发之路(十三)
    一个java程序员自学IOS开发之路(十二)
    一个java程序员自学IOS开发之路(十一)
    Swift学习笔记(六)
    一个java程序员自学IOS开发之路(十)
    Swift学习笔记(五)
    ubuntu服务器环境配置参考
    ubuntu添加新用户并添加管理员权限
    java GUI Graphics2D 绘图
    java链接数据库--Mysql
  • 原文地址:https://www.cnblogs.com/liuruipeng/p/7922125.html
Copyright © 2011-2022 走看看