MySQL的多表联合查询
一、MySQL多表联合查询介绍
MySQL多表联合查询包括内连接、外连接、笛卡尔积连接查询三种。今天我们通过实验来演示7种SQL JOINS的基本用法。
1.1 语法
常见的两表连接查询的基本语法结构如下:
select 表名.字段名,表名.字段名,表名.字段名,... from 左表表名 (as)左表别名 left/right/inner join 右表表名 (as) 右表别名 on 左表.连接字段=右表.连接字段 where 查询条件;
1.2 SQL执行顺序
(1)手写
SELECT DISTINCT <select_list> FROM <left table><join type> JoIN <right_table>oN<join_condition> WHERE <where_condition> GROUP BY <group_by_list> HAVING <having_condition> ORDER BY <order_by_condition> LIMIT <limit number>
(2)机读
FROM <left_table> ON <join_condition> <join_type> JOIN <right_table> WHERE <where_condition> GROUP BY <group_by_list> HAVING <having_condition> SELECT DISTINCT <select_list> ORDER BY <order_by_condition> LIMIT <limit_number>
二、MySQL多表联合查询演示
2.1 数据准备
CREATE DATABASE ucloud; USE ucloud; CREATE TABLE tbl_dept( id INT(11) NOT NULL AUTO_INCREMENT, deptName VARCHAR(30) DEFAULT NULL, locAdd VARCHAR(40) DEFAULT NULL, PRIMARY KEY(id) )ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; CREATE TABLE tbl_emp( id INT(11) NOT NULL AUTO_INCREMENT, NAME VARCHAR(20) DEFAULT NULL, deptld INT(11) DEFAULT NULL, PRIMARY KEY(id), KEY fk_dept_id(deptld) #CONSTRAINT fk_dept_id FOREIGN KEY(deptld) REFERENCES tbl_dept(id) )ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; INSERT INTO tbl_dept(deptName,locAdd) VALUES('RD',11); INSERT INTO tbl_dept(deptName,locAdd) VALUES('HR',12);213`0 INSERT INTO tbl_dept(deptName,locAdd) VALUES('MK',13)0; INSERT INTO tbl_dept(deptName,locAdd) VALUES('MIS',14); INSERT INTO tbl_dept(deptName,locAdd) VALUES('FD',15); INSERT INTO tbl_emp(NAME,deptld) VALUES('z3',1); INSERT INTO tbl_emp(NAME,deptld) VALUES('z4',1); INSERT INTO tbl_emp(NAME,deptld) VALUES('z5',1); INSERT INTO tbl_emp(NAME,deptld) VALUES('w5',2); INSERT INTO tbl_emp(NAME,deptld) VALUES('w6',2); INSERT INTO tbl_emp(NAME,deptld) VALUES('s7',3); INSERT INTO tbl_emp(NAME,deptld) VALUES('s8',4); INSERT INTO tbl_emp(NAME,deptld) VALUES('s9',51);
2.2 左连接left (outer) join
左连接的定义:左连接全称为左外连接,是外连接的一种。它是以左表为基础,根据ON后给出的连接条件将两表连接起来。结果会将左表所有的记录列出,而右表只列出ON后与左表满足条件的部分。
mysql> select * from tbl_emp a left join tbl_dept b on a.deptld=b.id;
mysql> select * from tbl_emp a left join tbl_dept b on a.deptld=b.id where b.id is null;
2.3 右连接right (outer) join
右连接的定义:右连接全称为右外连接,也是外连接的一种。与左连接刚好相反,它是以右表为基础,根据ON后给出的连接条件将两表连接起来。结果会将右表所有的记录列出,而左表只列出ON后与右表满足条件的部分。
mysql> select * from tbl_emp a right join tbl_dept b on a.deptld=b.id;
mysql> select * from tbl_emp a right join tbl_dept b on a.deptld=b.id where a.deptld is null;
2.4 内连接inner join
内连接是一种一一映射关系,即左右两张表都同时存在的记录才能显示出来,用韦恩图表示是两个集合的交集。
mysql> select * from tbl_emp a inner join tbl_dept b on a.deptld=b.id;
2.5 Other JOINS
mysql> select * from tbl_emp a left join tbl_dept b on a.deptld=b.id where b.id is null -> union -> select * from tbl_emp a right join tbl_dept b on a.deptld=b.id where a.deptld is null;
mysql> select * from tbl_emp a left join tbl_dept b on a.deptld=b.id -> union -> select * from tbl_emp a right join tbl_dept b on a.deptld=b.id;
select * from tbl_emp a full outer join tbl_dept b on a.deptld=b.id; 【注】MySQL不支持该语法,Oracle支持。
作者:UStarGao
链接:https://www.starcto.com/mysql/255.html
来源:STARCTO
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
UCloud云平台推荐
随便看看
- 2022-03-01Linux CPU与内存常见问题处理技巧
- 2022-05-27MongoDB慢查询日志之Profiling配置
- 2021-03-27MySQL GTID全局事物标识
- 2023-01-31UCloud MySQL innodbackup物理备份还原到本地
- 2021-08-22Smokeping+Promethues+Grafana搭建网络质量监控