zoukankan      html  css  js  c++  java
  • MySQL 的七种 join

    建表

    在这里呢我们先来建立两张有外键关联的张表。

    CREATE DATABASE db0206;
    USE db0206;
    
    CREATE TABLE `db0206`.`tbl_dept`(  
      `id` INT(11) NOT NULL AUTO_INCREMENT,
      `deptName` VARCHAR(30),
      `locAdd` VARCHAR(40),
      PRIMARY KEY (`id`)
    ) ENGINE=INNODB CHARSET=utf8;
    
    CREATE TABLE `db0206`.`tbl_emp`(  
      `id` INT(11) NOT NULL AUTO_INCREMENT,
      `name` VARCHAR(20),
      `deptId` INT(11),
      PRIMARY KEY (`id`),
      FOREIGN KEY (`deptId`) REFERENCES `db0206`.`tb_dept`(`id`)
    ) ENGINE=INNODB CHARSET=utf8;
    /*插入数据*/
    INSERT INTO tbl_dept(deptName,locAdd) VALUES('RD',11);
    INSERT INTO tbl_dept(deptName,locAdd) VALUES('HR',12);
    INSERT INTO tbl_dept(deptName,locAdd) VALUES('MK',13);
    INSERT INTO tbl_dept(deptName,locAdd) VALUES('MIS',14);
    INSERT INTO tbl_dept(deptName,locAdd) VALUES('FD',15);
    
    INSERT INTO tbl_emp(NAME,deptId) VALUES('z3',1);
    INSERT INTO tbl_emp(NAME,deptId) VALUES('z4',1);
    INSERT INTO tbl_emp(NAME,deptId) VALUES('z5',1);
    
    INSERT INTO tbl_emp(NAME,deptId) VALUES('w5',2);
    INSERT INTO tbl_emp(NAME,deptId) VALUES('w6',2);
    
    INSERT INTO tbl_emp(NAME,deptId) VALUES('s7',3);
    
    INSERT INTO tbl_emp(NAME,deptId) VALUES('s8',4);
    

    文氏图与SQL语句的编写以及查询结果

    内连接

    内连接文氏图

    表的内连接

    执行的sql语句以及执行的查询结果

    • 执行的sql语句
    select * from tbl_dept a inner join tbl_emp b on a.id=b.deptId;
    • 查询结果 
      这里写图片描述

    左外连接

    左外连接文氏图

    左连接

    执行的sql语句以及执行的查询结果

    • 执行的sql语句
    select * from tbl_dept a left join tbl_emp b on a.id=b.deptId;
    • 查询结果 
      左外连接

    右外连接

    右外连接文氏图

    这里写图片描述

    执行的sql语句以及执行的查询结果

    • 执行的sql语句
    select * from tbl_dept a right join tbl_emp b on a.id=b.deptId;
    • 查询结果 
      这里写图片描述

    左连接

    左连接文氏图

    这里写图片描述

    执行的sql语句以及执行的查询结果

    • 执行的sql语句
    elect * from tbl_dept a left join tbl_emp b on a.id=b.deptId where b.deptId is null;
    • 查询结果

    这里写图片描述

    右连接

    右连接文氏图

    右连接

    执行的sql语句以及执行的查询结果

    • 执行的sql语句
    select * from tbl_dept a right join tbl_emp b on a.id=b.deptId where a.id is null;
    • 查询结果

    右连接

    全连接

    全连接文氏图

    这里写图片描述

    执行的sql语句以及执行的查询结果

    • 执行的sql语句
    select * from tbl_dept a right join tbl_emp b on a.id=b.deptId 
    union 
    select * from tbl_dept a left join tbl_emp b on a.id=b.deptId;
    • 查询结果 
      全连接

    两张表中都没有出现的数据集

    文氏图

    执行的sql语句以及执行的查询结果

    • 执行的sql语句
    select * from tbl_dept a right join tbl_emp b on a.id=b.deptId where a.id is null union select * from tbl_dept a left join tbl_emp b on a.id=b.deptId where b.deptId is null;
    • 查询结果

    这里写图片描述

  • 相关阅读:
    几个论坛上看到的2015小米笔试题
    Line(扩展欧几里得)
    MapReduce编程之倒排索引
    annotation(@Retention@Target)详解
    【JEECG技术文档】JEECG平台对外接口JWT应用文档V3.7.2
    jeecg 模糊查询
    jeecg下实现自动默认模糊查询
    The packaging for this project did not assign a file to the build artifact
    Maven添加本地Jar包
    maven 如何引入本地jar包
  • 原文地址:https://www.cnblogs.com/dinglinyong/p/6656315.html
Copyright © 2011-2022 走看看