MySQL 7种 JOIN连表方法
规定:左边的圆代表表 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连表方法的更多相关文章
- MySQL中快速复制数据表方法汇总
本文将着重介绍两个MySQL命令的组合,它将以原有数据表为基础,创建相同结构和数据的新数据表. 这可以帮助你在开发过程中快速的复制表格作为测试数据,而不必冒险直接操作正在运行 的数据表. 示例如下: ...
- mysql中各种join连表查询总结
通常我们需要连接多个表查询数据,以获取想要的结果. 一.连接可以分为三类: (1) 内连接:join,inner join (2) 外连接:left join,left outer join,righ ...
- 什么是分表和分区 MySql数据库分区和分表方法
1.为什么要分表和分区 日常开发中我们经常会遇到大表的情况,所谓的大表是指存储了百万级乃至千万级条记录的表.这样的表过于庞大,导致数据库在查询和插入的时候耗时太长,性能低下,如果涉及联合查询的情况,性 ...
- Mysql七种 JOIN 连接
内连接 SELECT <select_list> FROM TableA A INNER JOIN TableB B ON A.Key = B.Key 左外连接 SELECT <se ...
- 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 ...
- 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 三. ...
- mysql left join 右表数据不唯一的情况解决方法
mysql left join 右表数据不唯一的情况解决方法 <pre>member 表id username1 fdipzone2 terry member_login_log 表id ...
- PostgreSQL EXPLAIN执行计划学习--多表连接几种Join方式比较
转了一部分.稍后再修改. 三种多表Join的算法: 一. NESTED LOOP: 对于被连接的数据子集较小的情况,嵌套循环连接是个较好的选择.在嵌套循环中,内表被外表驱动,外表返回的每一行都要在内表 ...
- MySql中4种批量更新的方法update table2,table1,批量更新用insert into ...on duplicate key update, 慎用replace into.
mysql 批量更新记录 MySql中4种批量更新的方法最近在完成MySql项目集成的情况下,需要增加批量更新的功能,根据网上的资料整理了一下,很好用,都测试过,可以直接使用. mysql 批量更新共 ...
随机推荐
- linux系统基础优化及高级操作命令
Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. ifconfig 查询.设置网卡和 ...
- 添加一个静态JAVA库
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Build all java files in the java subdirectory L ...
- 数据加密之AES
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- 利用Zookeeper实现分布式锁
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Spark学习(一)——Spark运行架构
基本概念 在具体讲解Spark运行架构之前,需要先了解几个重要的概念: RDD:是弹性分布式数据集(Resilient Distributed Dataset)的简称,是分布式内存的一个抽象概念,提供 ...
- jQuery .submit()
.submit() Events > Form Events | Forms .submit( handler )Returns: jQuery Description: Bind an eve ...
- Java 8:用Stream来循环集合
本文由 ImportNew - 进林 翻译自 deadcoderising.欢迎加入翻译小组.转载请见文末要求. 正如我之前所写的,Java 8中的新功能特性改变了游戏规则.对Java开发者来说这是一 ...
- LeetCode 409——最长回文串
1. 题目 2. 解答 我们先来看一看回文子串的规律,如果回文子串的长度为偶数,那么其中所有的每个元素都出现了偶数次:如果回文子串的长度为奇数,那么有一个元素出现了奇数次而其余每个元素都出现了偶数次. ...
- leetcode 714. 买卖股票的最佳时机含手续费
继承leetcode123以及leetcode309的思路,,但应该也可以写成leetcode 152. 乘积最大子序列的形式 class Solution { public: int maxProf ...
- PE盘制作
我这里以大白菜为例来讲解这个一条龙的服务: 1.去大白菜官网下载一U盘制作工具: 2.准备一个4G以上的U盘(我这里建议16G比较好,这样制作完成之后,我们还可以拷贝几个常用的镜像进去,方便今后使用) ...