• mysql中各种join连表查询总结


    通常我们需要连接多个表查询数据,以获取想要的结果。

    一、连接可以分为三类:

      (1) 内连接:join,inner join

      (2) 外连接:left join,left outer join,right join,right outer join,union,union all

      (3) 交叉连接:cross join

     

    二、准备需要演示的表:

    CREATE TABLE `a` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
      `a_name` varchar(32) DEFAULT '' COMMENT 'a表名称',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    
    CREATE TABLE `b` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
      `a_id` int(11) DEFAULT '0' COMMENT 'a表ID',
      `b_name` varchar(32) DEFAULT '' COMMENT 'b表名称',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    

    a表与b表的数据如图中所示:

    三、内连接inner join或join

    select * from a inner join b on a.id = b.a_id;
    select * from a join b on a.id = b.a_id;
    select * from a, b where a.id = b.a_id;
    

    结果如下:

    内连接可以理解为,两个表中同时满足某条件的数据记录组合。也就是表A和表B中满足条件a.id = b.a_id的所有记录。

    当表A中的一条记录对应表B中的多条记录时,会以重复的方式对应多条表B记录出现在结果集中。

    当表B中的一条记录对应表A中的多条记录时,会以重复的方式对应多条表A记录出现在结果集中。

    四、外连接left join或right join

      (1) 左外连接

    select * from a left join b on a.id = b.a_id;
    select * from a left outer join b on a.id = b.a_id;
    

    左外连接,会以左边的表A为主表,返回所有行,即使右表B中没有匹配的行。

    如果左边的表A在右表B中找不到一条记录,则返回表A所有记录并且表B相应的字段设为null。

    如果左边的表A在右表B中找到多条记录,则以相同表A记录和不同表B记录多条显示在结果集中。

    这种情况下,其实是把表A中所有记录都查询出来了,包括不满足条件的记录。

    如果我们只想查出表A中满足条件的,或是不满足条件的,该怎么查?

    select * from a left join b on a.id = b.a_id where b.a_id is not null;
    select * from a left outer join b on a.id = b.a_id where b.a_id is not null;
    

    上面的语句查询的,就是表A中满足条件的。

    select * from a left join b on a.id = b.a_id where b.a_id is null;
    select * from a left outer join b on a.id = b.a_id where b.a_id is null;
    

    上面的语句查询的,就是表A中不满足条件的。

      (2) 右外连接

    select * from a right join b on a.id = b.a_id;
    select * from a right outer join b on a.id = b.a_id;
    

    右外连接其实跟左外连接一样,区别在于 主表的确定,两者之间可以相互转换。

    右外连接的描述基本与左外连接相同,这里就不过多描述了。

      (3) 全连接full join

    mysql并不支持全连接,不过有相应的替代方案,就是left join union right join 来代替。

    select * from a left join b on a.id = b.a_id
    union
    select * from a right join b on a.id = b.a_id;
    

    全连接会从表A和表B中返回所有的行,如果表A中的行在表B中没有匹配,或是表B中的行在表A中没有匹配,这些行都会显示,不存在的字段以null补充。

    union会把其中重复的行合并。

    这种情况下,是把表A和表B中满足条件和不满足条件的记录都显示出来了。

    如果只想显示所有不满足条件的记录,则通过如下语句:

    select * from a left join b on a.id = b.a_id where b.a_id is null
    union
    select * from a right join b on a.id = b.a_id where a.id is null;
    

    如果只想显示所有满足条件的记录,则通过如下语句:

    select * from a left join b on a.id = b.a_id where b.a_id is not null
    union
    select * from a right join b on a.id = b.a_id where a.id is not null;
    

      

     五、交叉连接

    交叉连接实际上就是表A与表B的笛卡尔乘积。

    select * from a cross join b;
    select * from a, b;
    

  • 相关阅读:
    Java基础知识强化80:Math类random()方法的小扩展(控制产生目的数字的概率)
    Java基础知识强化79:被遗忘的Java Math类
    Java基础知识强化78:正则表达式之获取功能(案例)
    Java基础知识强化77:正则表达式之获取功能(Pattern 和 Matcher类的使用)
    Java基础知识强化76:正则表达式之替换功能
    Java基础知识强化75:正则表达式之分割功能(字符串中的数字排序案例)
    asp.net页面过滤所有换行符和多余空格
    nvelocity学习笔记
    ASP.NET session时间的设置
    Server的Transfer和Response的Redirect
  • 原文地址:https://www.cnblogs.com/jkko123/p/10129154.html
Copyright © 2020-2023  润新知