2020 11.17曾宇欣

知识点:

多表联查

数据准备

#建库
create database test;

#建表
create table department(
id int,
name varchar(20) 
);

create table staff(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') not null default 'male',
age int,
dep_id int
);

#插入数据
insert into department values
(200,'挖矿小分队'),
(201,'人力资源'),
(202,'销售'),
(203,'运营');

insert into staff(name,sex,age,dep_id) values
('程咬金','male',38,200),
('露娜','female',26,201),
('李白','male',38,201),
('王昭君','female',28,202),
('典韦','male',118,200),
('小乔','female',16,204)
;

#查看表结构和数据
mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.06 sec)

mysql> desc staff;
+--------+-----------------------+------+-----+---------+----------------+
| Field  | Type                  | Null | Key | Default | Extra          |
+--------+-----------------------+------+-----+---------+----------------+
| id     | int(11)               | NO   | PRI | NULL    | auto_increment |
| name   | varchar(20)           | YES  |     | NULL    |                |
| sex    | enum('male','female') | NO   |     | male    |                |
| age    | int(11)               | YES  |     | NULL    |                |
| dep_id | int(11)               | YES  |     | NULL    |                |
+--------+-----------------------+------+-----+---------+----------------+
5 rows in set (0.04 sec)

#表department与staff
mysql> select * from department;
+------+-----------------+
| id   | name            |
+------+-----------------+
|  200 | 挖矿小分队      |
|  201 | 人力资源        |
|  202 | 销售            |
|  203 | 运营            |
+------+-----------------+
4 rows in set (0.00 sec)

mysql> select * from staff;
+----+-----------+--------+------+--------+
| id | name      | sex    | age  | dep_id |
+----+-----------+--------+------+--------+
|  1 | 程咬金    | male   |   38 |    200 |
|  2 | 露娜      | female |   26 |    201 |
|  3 | 李白      | male   |   38 |    201 |
|  4 | 王昭君    | female |   28 |    202 |
|  5 | 典韦      | male   |  118 |    200 |
|  6 | 小乔      | female |   16 |    204 |
+----+-----------+--------+------+--------+
6 rows in set (0.00 sec)

连表查

  • 交叉连接 不适用任何匹配条件 会产生笛卡尔积
  • select * from 表1,表2
+----+--------+--------+------+--------+------+------------+
| id | name   | sex    | age  | dep_id | id   | name       |
+----+--------+--------+------+--------+------+------------+
|  1 | 程咬金 | male   |   38 |    200 |  200 | 挖矿小分队 |
|  1 | 程咬金 | male   |   38 |    200 |  201 | 人力资源   |
|  1 | 程咬金 | male   |   38 |    200 |  202 | 销售       |
|  1 | 程咬金 | male   |   38 |    200 |  203 | 运营       |
|  2 | 露娜   | female |   26 |    201 |  200 | 挖矿小分队 |
|  2 | 露娜   | female |   26 |    201 |  201 | 人力资源   |
|  2 | 露娜   | female |   26 |    201 |  202 | 销售       |
|  2 | 露娜   | female |   26 |    201 |  203 | 运营       |
|  3 | 李白   | male   |   38 |    201 |  200 | 挖矿小分队 |
|  3 | 李白   | male   |   38 |    201 |  201 | 人力资源   |
|  3 | 李白   | male   |   38 |    201 |  202 | 销售       |
|  3 | 李白   | male   |   38 |    201 |  203 | 运营       |
|  4 | 王昭君 | female |   28 |    202 |  200 | 挖矿小分队 |
|  4 | 王昭君 | female |   28 |    202 |  201 | 人力资源   |
|  4 | 王昭君 | female |   28 |    202 |  202 | 销售       |
|  4 | 王昭君 | female |   28 |    202 |  203 | 运营       |
|  5 | 典韦   | male   |  118 |    200 |  200 | 挖矿小分队 |
|  5 | 典韦   | male   |  118 |    200 |  201 | 人力资源   |
|  5 | 典韦   | male   |  118 |    200 |  202 | 销售       |
|  5 | 典韦   | male   |  118 |    200 |  203 | 运营       |
|  6 | 小乔   | female |   16 |    204 |  200 | 挖矿小分队 |
|  6 | 小乔   | female |   16 |    204 |  201 | 人力资源   |
|  6 | 小乔   | female |   16 |    204 |  202 | 销售       |
|  6 | 小乔   | female |   16 |    204 |  203 | 运营       |
+----+--------+--------+------+--------+------+------------+
  • 内连接(常用)只连接匹配的行

  • select * from 表1 innner join department on 条件(表1.字段=表2.字段)

select * from staff inner join department on staff.dep_id = department.id;
+----+--------+--------+------+--------+------+------------+
| id | name   | sex    | age  | dep_id | id   | name       |
+----+--------+--------+------+--------+------+------------+
|  1 | 程咬金 | male   |   38 |    200 |  200 | 挖矿小分队 |
|  2 | 露娜   | female |   26 |    201 |  201 | 人力资源   |
|  3 | 李白   | male   |   38 |    201 |  201 | 人力资源   |
|  4 | 王昭君 | female |   28 |    202 |  202 | 销售       |
|  5 | 典韦   | male   |  118 |    200 |  200 | 挖矿小分队 |
+----+--------+--------+------+--------+------+------------+
  • 外连接
  • 左外连接(常用) 优先显示左表的全部记录 left join
  • select * from staff left join department on 条件(表1.字段=表2.字段)
  • 右外连接 优先显示右表全部记录 right join
  • select * from 表1 right join department on 条件(表1.字段 = 表2.字段)
  • 全外连接 显示左右两表的全部记录
  • 查询语句(左外连接 + 右外连接来实现)
  • select * from table1 left join table2 on 条件 union select * from table1 right join table2 on 条件

标签

评论

this is is footer