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 | +------+--------+------+--------+