MySql join那些事

创建数据库

create database business;
use business;

创建表

create table dept(
 id int(11) not null auto_increment,
 deptName varchar(30) default null,
 floor varchar(40) default null,
 primary key(id)
) engine=innodb auto_increment=1 default charset=utf8;

create table emp(
 id int(11) not null auto_increment,
 name varchar(20) default null,
 deptId int(11) default null,
 primary key(id)
) engine=innodb auto_increment=1 default charset=utf8;

insert into dept(deptName,floor) values('Develop',1);
insert into dept(deptName,floor) values('HuamanResource',2);
insert into dept(deptName,floor) values('Market',3);

insert into emp(name,deptId) values('zs',1);
insert into emp(name,deptId) values('ls',1);
insert into emp(name,deptId) values('ww',1);
insert into emp(name,deptId) values('zl',2);
insert into emp(name,deptId) values('qq',4);

员工表

select * from emp;
+----+------+--------+
| id | name | deptId |
+----+------+--------+
|  1 | zs   |      1 |
|  2 | ls   |      1 |
|  3 | ww   |      1 |
|  4 | zl   |      2 |
|  5 | qq   |      4 |
+----+------+--------+
5 rows in set (0.02 sec)

部门表

select * from dept;
+----+----------------+-------+
| id | deptName       | floor |
+----+----------------+-------+
|  1 | Develop        | 1     |
|  2 | HuamanResource | 2     |
|  3 | Market         | 3     |
+----+----------------+-------+
3 rows in set (0.02 sec)

select * from dept,emp;  -- 笛卡尔积
+----+----------------+-------+----+------+--------+
| id | deptName       | floor | id | name | deptId |
+----+----------------+-------+----+------+--------+
|  1 | Develop        | 1     |  1 | zs   |      1 |
|  2 | HuamanResource | 2     |  1 | zs   |      1 |
|  3 | Market         | 3     |  1 | zs   |      1 |
|  1 | Develop        | 1     |  2 | ls   |      1 |
|  2 | HuamanResource | 2     |  2 | ls   |      1 |
|  3 | Market         | 3     |  2 | ls   |      1 |
|  1 | Develop        | 1     |  3 | ww   |      1 |
|  2 | HuamanResource | 2     |  3 | ww   |      1 |
|  3 | Market         | 3     |  3 | ww   |      1 |
|  1 | Develop        | 1     |  4 | zl   |      2 |
|  2 | HuamanResource | 2     |  4 | zl   |      2 |
|  3 | Market         | 3     |  4 | zl   |      2 |
|  1 | Develop        | 1     |  5 | qq   |      4 |
|  2 | HuamanResource | 2     |  5 | qq   |      4 |
|  3 | Market         | 3     |  5 | qq   |      4 |
+----+----------------+-------+----+------+--------+
15 rows in set (0.00 sec)
select * from  emp a inner join dept b on a.deptId=b.id; -- 公有
+----+------+--------+----+----------------+-------+
| id | name | deptId | id | deptName       | floor |
+----+------+--------+----+----------------+-------+
|  1 | zs   |      1 |  1 | Develop        | 1     |
|  2 | ls   |      1 |  1 | Develop        | 1     |
|  3 | ww   |      1 |  1 | Develop        | 1     |
|  4 | zl   |      2 |  2 | HuamanResource | 2     |
+----+------+--------+----+----------------+-------+
4 rows in set (0.01 sec)

公有

select * from  emp a left join dept b on a.deptId=b.id; -- 全a
+----+------+--------+------+----------------+-------+
| id | name | deptId | id   | deptName       | floor |
+----+------+--------+------+----------------+-------+
|  1 | zs   |      1 |    1 | Develop        | 1     |
|  2 | ls   |      1 |    1 | Develop        | 1     |
|  3 | ww   |      1 |    1 | Develop        | 1     |
|  4 | zl   |      2 |    2 | HuamanResource | 2     |
|  5 | qq   |      4 | NULL | NULL           | NULL  |
+----+------+--------+------+----------------+-------+
5 rows in set (0.01 sec)

全a

select * from  emp a right join dept b on a.deptId=b.id; -- 全b
+------+------+--------+----+----------------+-------+
| id   | name | deptId | id | deptName       | floor |
+------+------+--------+----+----------------+-------+
|    1 | zs   |      1 |  1 | Develop        | 1     |
|    2 | ls   |      1 |  1 | Develop        | 1     |
|    3 | ww   |      1 |  1 | Develop        | 1     |
|    4 | zl   |      2 |  2 | HuamanResource | 2     |
| NULL | NULL |   NULL |  3 | Market         | 3     |
+------+------+--------+----+----------------+-------+
5 rows in set (0.00 sec)

全b

select * from  emp a left join dept b on a.deptId=b.id where b.id is null; -- 独a
+----+------+--------+------+----------+-------+
| id | name | deptId | id   | deptName | floor |
+----+------+--------+------+----------+-------+
|  5 | qq   |      4 | NULL | NULL     | NULL  |
+----+------+--------+------+----------+-------+
1 row in set (0.01 sec)

独a

select * from  emp a right join dept b on a.deptId=b.id where a.deptId is null; -- 独b
+------+------+--------+----+----------+-------+
| id   | name | deptId | id | deptName | floor |
+------+------+--------+----+----------+-------+
| NULL | NULL |   NULL |  3 | Market   | 3     |
+------+------+--------+----+----------+-------+
1 row in set (0.00 sec)

独b

select * from  emp a left join dept b on a.deptId=b.id
union
select * from  emp a right join dept b on a.deptId=b.id; -- 全有
+------+------+--------+------+----------------+-------+
| id   | name | deptId | id   | deptName       | floor |
+------+------+--------+------+----------------+-------+
|    1 | zs   |      1 |    1 | Develop        | 1     |
|    2 | ls   |      1 |    1 | Develop        | 1     |
|    3 | ww   |      1 |    1 | Develop        | 1     |
|    4 | zl   |      2 |    2 | HuamanResource | 2     |
|    5 | qq   |      4 | NULL | NULL           | NULL  |
| NULL | NULL |   NULL |    3 | Market         | 3     |
+------+------+--------+------+----------------+-------+
6 rows in set (0.02 sec)

全有

select * from  emp a left join dept b on a.deptId=b.id where b.id is null
union
select * from  emp a right join dept b on a.deptId=b.id where a.deptId is null; -- a、b两者独有
+------+------+--------+------+----------+-------+
| id   | name | deptId | id   | deptName | floor |
+------+------+--------+------+----------+-------+
|    5 | qq   |      4 | NULL | NULL     | NULL  |
| NULL | NULL |   NULL |    3 | Market   | 3     |
+------+------+--------+------+----------+-------+
2 rows in set (0.00 sec)

两者独有

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值