规定:左边的圆代表表 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 |
+------+--------+------+--------+

最新文章

  1. How to convert any valid date string to a DateTime.
  2. windows核心编程---第一章 谈谈windows中的错误处理机制
  3. ServiceManager 小结
  4. Spring security3
  5. 《linux下sudo服务的使用》RHEL6
  6. (转)C#模拟键盘鼠标事件
  7. mssql sql高效关联子查询的update 批量更新
  8. H.264 RTPpayload 格式------ H.264 视频 RTP 负载格式
  9. uVa 714 (二分法)
  10. T-SQL基础(5) - 表表达式
  11. hashChange &amp; url change &amp; QRCode &amp; canvas to image
  12. Linux中断管理
  13. iview 表单非空验证
  14. Mysql多实例安装笔记
  15. SqlBulkCopy简单封装,让批量插入更方便
  16. JSONObject基本内容(二)
  17. LoadRunner改脚本
  18. JAVA设计模式详解(三)----------装饰者模式
  19. SSH安装篇之——SecureCRT连接(内网和外网)虚拟机中的Linux系统(Ubuntu)
  20. win10上走网络打印机(不需找驱动包,会自动)

热门文章

  1. Spring理解?
  2. SpringBoot中application.yml基本配置详情
  3. Servlet基础总结
  4. golang网络通信超时设置
  5. anaconda 安装pyspider出错
  6. VS 2017 VC++项目出现 LNK1104 无法打开文件&quot;libcmtd.lib&quot; 的解决方法
  7. MyBatis如何传入多个参数
  8. 四十八:数据库之alembic常用命令和经典错误的解决办法
  9. SAE Django如何禁止外部IP访问
  10. activiti随笔记录