数据库教程:MySQL 四种连接和多表查询详解

目录mysql 内连接、左连接、右连接、外连接、多表查询一、inner jion 内连接 ( a ∩ b )二、left join 左外连接( a 全有 )三、right join 右外连接 (b 全

目录
  • mysql 内连接、左连接、右连接、外连接、多表查询
    • 一、inner jion 内连接 ( a ∩ b )
    • 二、left join 左外连接( a 全有 )
    • 三、right join 右外连接 (b 全有)
    • 四、full join 全外连接( a + b)
    • 五、left excluding join ( a – b 即 a 表独有)+
    • 六、right excluding join ( b – a 即 b表独有)
    • 七、outer excluding join (a 与 b 各自独有)

    mysql 内连接、左连接、右连接、外连接、多表查询

    构建环境:

      create table t_emp(  	id int primary key,   	name varchar(20),  	deptid int  );  create table t_dept(  	id int primary key,  	name varchar(20)  );  insert into t_dept(id, name) values(1, '设计部');  insert into t_dept(id, name) values(2, '开发部');  insert into t_dept(id, name) values(3, '测试部');  insert into t_emp(id, name, deptid) values(1, '张三', 1);  insert into t_emp(id, name, deptid) values(2, '李四', 2);  insert into t_emp(id, name, deptid) values(3, '王五', 0);  # ps:为了说明方便,t_emp 表 说成 a 表, t_dept 表说成 b 表

    目录

    一、inner jion 内连接 ( a ∩ b )

    MySQL 四种连接和多表查询详解

      select * from t_emp e inner join t_dept d on  e.deptid = d.id;

    MySQL 四种连接和多表查询详解

    二、left join 左外连接( a 全有 )

    MySQL 四种连接和多表查询详解

      select * from t_emp e left join t_dept d on e.deptid = d.id;

    MySQL 四种连接和多表查询详解

    三、right join 右外连接 (b 全有)

    MySQL 四种连接和多表查询详解

      select * from t_emp e right join t_dept d on e.deptid = d.id;

    MySQL 四种连接和多表查询详解

    四、full join 全外连接( a + b)

    MySQL 四种连接和多表查询详解

      select * from t_emp e left join t_dept d   on e.deptid = d.id union   select * from t_emp e right join t_dept d on e.deptid = d.id;

    MySQL 四种连接和多表查询详解

    五、left excluding join ( a – b 即 a 表独有)+

    MySQL 四种连接和多表查询详解

      select * from t_emp e left join t_dept d on e.deptid= d.id where d.id is null;

    MySQL 四种连接和多表查询详解

    六、right excluding join ( b – a 即 b表独有)

    MySQL 四种连接和多表查询详解

      select * from t_emp e right join t_dept d on e.deptid= d.id where e.id is null;

    MySQL 四种连接和多表查询详解

    七、outer excluding join (a 与 b 各自独有)

    MySQL 四种连接和多表查询详解

      select * from t_emp e left join t_dept d on e.deptid= d.id where d.id is null  union  select * from t_emp e right join t_dept d on e.deptid= d.id where e.id is null;

    MySQL 四种连接和多表查询详解

    总结

    本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注<计算机技术网(www.ctvol.com)!!>的更多内容!

    需要了解更多数据库技术:MySQL 四种连接和多表查询详解,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

    本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

    ctvol管理联系方式QQ:251552304

    本文章地址:https://www.ctvol.com/dtteaching/807394.html

    (0)
    上一篇 2021年9月10日
    下一篇 2021年9月10日

    精彩推荐