规定:左边的圆代表表 a,右边的代表 b。

JOIN 关键字可以在两表之间选中任意部分。】

通过以下代码制造一些数据:

delimiter //
drop procedure if exists produce_data//
create procedure produce_data()
begin
declare i int default 0;
drop table if exists a;
drop table if exists b;
create table a(id int not null,name varchar(32));
create table b(id int not null,name varchar(32));
set i = 1;
while i <= 4 do
insert into a(id,name) values(i, concat('name', i));
set i = i + 1;
end while;
set i = 3;
while i <= 6 do
insert into b(id,name) values(i, concat('name', i));
set i = i + 1;
end while;
end//
call produce_data()//
mysql> select * from a//
+----+-------+
| id | name |
+----+-------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
| 4 | name4 |
+----+-------+
mysql> select * from b//
+----+-------+
| id | name |
+----+-------+
| 3 | name3 |
| 4 | name4 |
| 5 | name5 |
| 6 | name6 |
+----+-------+

select *
from a left join b on a.id = b.id// +----+-------+------+-------+
| id | name | id | name |
+----+-------+------+-------+
| 1 | name1 | NULL | NULL |
| 2 | name2 | NULL | NULL |
| 3 | name3 | 3 | name3 |
| 4 | name4 | 4 | name4 |
+----+-------+------+-------+

select *
from a right outer join b on a.id = b.id//
+------+-------+----+-------+
| id | name | id | name |
+------+-------+----+-------+
| 3 | name3 | 3 | name3 |
| 4 | name4 | 4 | name4 |
| NULL | NULL | 5 | name5 |
| NULL | NULL | 6 | name6 |
+------+-------+----+-------+

select *
from a inner join b on a.id = b.id// +----+-------+----+-------+
| id | name | id | name |
+----+-------+----+-------+
| 3 | name3 | 3 | name3 |
| 4 | name4 | 4 | name4 |
+----+-------+----+-------+

select *
from a left join b on a.id = b.id
where b.id is null// +----+-------+------+------+
| id | name | id | name |
+----+-------+------+------+
| 1 | name1 | NULL | NULL |
| 2 | name2 | NULL | NULL |
+----+-------+------+------+

select *
from a right join b on a.id = b.id
where a.id is null// +------+------+----+-------+
| id | name | id | name |
+------+------+----+-------+
| NULL | NULL | 5 | name5 |
| NULL | NULL | 6 | name6 |
+------+------+----+-------+

一般,是这样写:

select *
from a full outer join b on a.id = b.id
where a.id is null or b.id is null//

但是,mysql 并没有 FULL 关键字,因此使用 UNION 联接 左连接和 右连接。

select *
from a left join b on a.id = b.id
where b.id is null
union
select *
from a right join b on a.id = b.id
where a.id is null//
+------+--------+------+--------+
| a_id | a_name | b_id | b_name |
+------+--------+------+--------+
| 1 | name1 | NULL | NULL |
| 2 | name2 | NULL | NULL |
| NULL | NULL | 5 | name5 |
| NULL | NULL | 6 | name6 |
+------+--------+------+--------+

类似上面,使用UNION

select a.id a_id, a.name a_name, b.id b_id, b.name b_name
from a left join b on a.id = b.id
union
select a.id a_id, a.name a_name, b.id b_id, b.name b_name
from a right join b on a.id = b.id//
+------+--------+------+--------+
| a_id | a_name | b_id | b_name |
+------+--------+------+--------+
| 1 | name1 | NULL | NULL |
| 2 | name2 | NULL | NULL |
| 3 | name3 | 3 | name3 |
| 4 | name4 | 4 | name4 |
| NULL | NULL | 5 | name5 |
| NULL | NULL | 6 | name6 |
+------+--------+------+--------+

MySQL 7种 JOIN连表方法的更多相关文章

  1. MySQL中快速复制数据表方法汇总

    本文将着重介绍两个MySQL命令的组合,它将以原有数据表为基础,创建相同结构和数据的新数据表. 这可以帮助你在开发过程中快速的复制表格作为测试数据,而不必冒险直接操作正在运行 的数据表. 示例如下: ...

  2. mysql中各种join连表查询总结

    通常我们需要连接多个表查询数据,以获取想要的结果. 一.连接可以分为三类: (1) 内连接:join,inner join (2) 外连接:left join,left outer join,righ ...

  3. 什么是分表和分区 MySql数据库分区和分表方法

    1.为什么要分表和分区 日常开发中我们经常会遇到大表的情况,所谓的大表是指存储了百万级乃至千万级条记录的表.这样的表过于庞大,导致数据库在查询和插入的时候耗时太长,性能低下,如果涉及联合查询的情况,性 ...

  4. Mysql七种 JOIN 连接

    内连接 SELECT <select_list> FROM TableA A INNER JOIN TableB B ON A.Key = B.Key 左外连接 SELECT <se ...

  5. MySQL七种join理论

    1. 内连接 select * from A inner join B where A.key=B.key; 2. 左连接 select * from A left join B on A.key=B ...

  6. mysql 7 种 join

    一. select * from A inner join B on A.key = B.key 二. select * from A left join B on A.key = B.key 三. ...

  7. mysql left join 右表数据不唯一的情况解决方法

    mysql left join 右表数据不唯一的情况解决方法 <pre>member 表id username1 fdipzone2 terry member_login_log 表id ...

  8. PostgreSQL EXPLAIN执行计划学习--多表连接几种Join方式比较

    转了一部分.稍后再修改. 三种多表Join的算法: 一. NESTED LOOP: 对于被连接的数据子集较小的情况,嵌套循环连接是个较好的选择.在嵌套循环中,内表被外表驱动,外表返回的每一行都要在内表 ...

  9. MySql中4种批量更新的方法update table2,table1,批量更新用insert into ...on duplicate key update, 慎用replace into.

    mysql 批量更新记录 MySql中4种批量更新的方法最近在完成MySql项目集成的情况下,需要增加批量更新的功能,根据网上的资料整理了一下,很好用,都测试过,可以直接使用. mysql 批量更新共 ...

随机推荐

  1. 讨厌的adb占用

    adb重启的方法 2018年06月13日 14:59:47 丽闪无敌 阅读数:11969   执行以下命令: D:\android-sdks\platform-tools>adb kill-se ...

  2. python3笔记十二:python数据类型-Dictionary字典

    一:学习内容 字典概念 字典创建 字典访问 字典添加 字典删除 字典遍历 字典与列表比较 二:字典概念 1.使用键值对(key-value)存储,具有极快的查找速度 2.注意:字典是无序的 3.特性: ...

  3. i 是一个修饰符 (搜索不区分大小写)

    什么是正则表达式? 正则表达式是由一个字符序列形成的搜索模式. 当你在文本中搜索数据时,你可以用搜索模式来描述你要查询的内容. 正则表达式可以是一个简单的字符,或一个更复杂的模式. 正则表达式可用于所 ...

  4. 七、smarty--缓存的控制

    1.建议缓存 $smarty->cacheing = true; //设置为2是给每一个模板设置缓存 $smarty->setCacheDir(“”); 2.处理缓存的生命周期 $smar ...

  5. What is the most efficient way to deep clone an object in JavaScript?

    What is the most efficient way to deep clone an object in JavaScript? Reliable cloning using a libra ...

  6. JAVA-retry 重试

    在看 ThreadPoolExecutor 源码时看到这么一段代码 retry: for (;;) { int c = ctl.get(); int rs = runStateOf(c); // Ch ...

  7. leetcode-easy-others-268 Missing Number

    mycode   80.25% class Solution(object): def missingNumber(self, nums): """ :type nums ...

  8. DB2日常管理

    执行时间最长的10条SQL语句(按时间降序排列),可保存为脚本方便调用:db2 "SELECT rows_read / (num_executions + 1) as avg_rows_re ...

  9. delphi 导出excel

    Var FExcel:OleVariant; //excel应用程序 FWorkBook :OleVariant; //工作表 Temsheet:OleVariant; //工作薄 FPicture: ...

  10. C#代码获取另一程序的错误提示,并关闭窗口。

    A程序报错弹框如下: B程序捕捉到此错误消息,并关闭.B程序核心代码如下. private void timer_Click(object sender, EventArgs e) { //查找Mes ...