MySQL学习(四) SQL连接查询
更多情况下,我们查询的数据来源于多张表,所有有必要了解一下MySQL中的连接查询。
SQL中将连接查询分成四类:交叉连接,内连接,外连接和自然连接。
数据准备
student表
-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`age` tinyint(4) DEFAULT NULL,
`classId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 's1', '20', '1');
INSERT INTO `student` VALUES ('2', 's2', '22', '1');
INSERT INTO `student` VALUES ('3', 's3', '22', '2');
INSERT INTO `student` VALUES ('4', 's4', '22', null);
class表
-- ----------------------------
-- Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of class
-- ----------------------------
INSERT INTO `class` VALUES ('1', '一班');
INSERT INTO `class` VALUES ('2', '二班');
INSERT INTO `class` VALUES ('3', '');
score表
-- ----------------------------
-- Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`score` decimal(4,1) DEFAULT NULL,
`studentId` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES ('1', '语文', '90.0', '1');
INSERT INTO `score` VALUES ('2', '数学', '95.0', '1');
INSERT INTO `score` VALUES ('3', '语文', '92.0', '2');
INSERT INTO `score` VALUES ('4', '数学', '88.0', '2');
INSERT INTO `score` VALUES ('5', '语文', '96.0', '3');
INSERT INTO `score` VALUES ('6', '数学', null, '3');
交叉连接
交叉连接(CROSS JOIN)是用左表中的每一行与右表中的每一行进行连接,不能使用ON关键字。所得到的结果将是这两个表中各行数据的所有组合,即这两个表所有数据的笛卡尔积。如果A表有4条记录,B表有3条,则结果有4*3=12条记录。
mysql> SELECT * FROM student CROSS JOIN class;
+----+------+-----+---------+----+-------+
| id | name | age | classId | id | cname |
+----+------+-----+---------+----+-------+
| 1 | s1 | 20 | 1 | 1 | 一班 |
| 1 | s1 | 20 | 1 | 2 | 二班 |
| 1 | s1 | 20 | 1 | 3 | |
| 2 | s2 | 22 | 1 | 1 | 一班 |
| 2 | s2 | 22 | 1 | 2 | 二班 |
| 2 | s2 | 22 | 1 | 3 | |
| 3 | s3 | 22 | 2 | 1 | 一班 |
| 3 | s3 | 22 | 2 | 2 | 二班 |
| 3 | s3 | 22 | 2 | 3 | |
| 4 | s4 | 22 | NULL | 1 | 一班 |
| 4 | s4 | 22 | NULL | 2 | 二班 |
| 4 | s4 | 22 | NULL | 3 | |
+----+------+-----+---------+----+-------+
12 rows in set
如果给交叉连接加上WHERE关键字,此时将返回符合条件的结果集,这时候与内连接的执行结果一样。
mysql> SELECT * FROM student CROSS JOIN class WHERE student.classId = class.id;
+----+------+-----+---------+----+-------+
| id | name | age | classId | id | cname |
+----+------+-----+---------+----+-------+
| 1 | s1 | 20 | 1 | 1 | 一班 |
| 2 | s2 | 22 | 1 | 1 | 一班 |
| 3 | s3 | 22 | 2 | 2 | 二班 |
+----+------+-----+---------+----+-------+
3 rows in set
内连接
内连接(INNER JOIN)是用左表中的每一行与右表中的所有记录进行匹配,查询的结果为两个表经过ON条件过滤后的笛卡尔积
mysql> SELECT * FROM student INNER JOIN class ON student.classId = class.id; -- 推荐写法,INNER可写可不写
+----+------+-----+---------+----+-------+
| id | name | age | classId | id | cname |
+----+------+-----+---------+----+-------+
| 1 | s1 | 20 | 1 | 1 | 一班 |
| 2 | s2 | 22 | 1 | 1 | 一班 |
| 3 | s3 | 22 | 2 | 2 | 二班 |
+----+------+-----+---------+----+-------+
3 rows in set
等价于
mysql> SELECT * FROM student, class WHERE student.classId = class.id;
+----+------+-----+---------+----+------+
| id | name | age | classId | id | name |
+----+------+-----+---------+----+------+
| 1 | s1 | 20 | 1 | 1 | 一班 |
| 2 | s2 | 22 | 1 | 1 | 一班 |
| 3 | s3 | 22 | 2 | 2 | 二班 |
+----+------+-----+---------+----+------+
3 rows in set
可以连接多张表
mysql> SELECT * FROM student
-> JOIN class ON student.classId = class.id
-> JOIN score ON student.id = score.studentId;
+----+------+-----+---------+----+-------+----+------+-------+-----------+
| id | name | age | classId | id | cname | id | name | score | studentId |
+----+------+-----+---------+----+-------+----+------+-------+-----------+
| 1 | s1 | 20 | 1 | 1 | 一班 | 1 | 语文 | 90 | 1 |
| 1 | s1 | 20 | 1 | 1 | 一班 | 2 | 数学 | 95 | 1 |
| 2 | s2 | 22 | 1 | 1 | 一班 | 3 | 语文 | 92 | 2 |
| 2 | s2 | 22 | 1 | 1 | 一班 | 4 | 数学 | 88 | 2 |
| 3 | s3 | 22 | 2 | 2 | 二班 | 5 | 语文 | 96 | 3 |
| 3 | s3 | 22 | 2 | 2 | 二班 | 6 | 数学 | NULL | 3 |
+----+------+-----+---------+----+-------+----+------+-------+-----------+
6 rows in set
外连接
左外连接
左外连接包含LEFT JOIN左表所有行,如果左表中某行在右表没有匹配,则结果中对应行右表的部分全部为空(NULL)。
mysql> SELECT * FROM student LEFT JOIN class ON student.classId = class.id; -- 或者LEFT OUTER JOIN
+----+------+-----+---------+------+-------+
| id | name | age | classId | id | cname |
+----+------+-----+---------+------+-------+
| 1 | s1 | 20 | 1 | 1 | 一班 |
| 2 | s2 | 22 | 1 | 1 | 一班 |
| 3 | s3 | 22 | 2 | 2 | 二班 |
| 4 | s4 | 22 | NULL | NULL | NULL |
+----+------+-----+---------+------+-------+
4 rows in set
右外连接
右外连接包含RIGHT JOIN左表所有行,如果右表中某行在左表没有匹配,则结果中对应行左表的部分全部为空(NULL)。
mysql> SELECT * FROM student RIGHT JOIN class ON student.classId = class.id; -- 或者RIGHT OUTER JOIN
+------+------+------+---------+----+-------+
| id | name | age | classId | id | cname |
+------+------+------+---------+----+-------+
| 1 | s1 | 20 | 1 | 1 | 一班 |
| 2 | s2 | 22 | 1 | 1 | 一班 |
| 3 | s3 | 22 | 2 | 2 | 二班 |
| NULL | NULL | NULL | NULL | 3 | |
+------+------+------+---------+----+-------+
4 rows in set
全外连接
如果左表中某行在右表没有匹配,则结果中对应行右表的部分全部为空(NULL),如果右表中某行在左表没有匹配,则结果中对应行左表的部分全部为空(NULL)。MySQL不支持FULL JOIN,但是我们可以对左连接和右连接的结果做UNION操作来实现
mysql> SELECT * FROM student LEFT JOIN class ON student.classId = class.id
-> UNION
-> SELECT * FROM student RIGHT JOIN class ON student.classId = class.id;
+------+------+------+---------+------+-------+
| id | name | age | classId | id | cname |
+------+------+------+---------+------+-------+
| 1 | s1 | 20 | 1 | 1 | 一班 |
| 2 | s2 | 22 | 1 | 1 | 一班 |
| 3 | s3 | 22 | 2 | 2 | 二班 |
| 4 | s4 | 22 | NULL | NULL | NULL |
| NULL | NULL | NULL | NULL | 3 | |
+------+------+------+---------+------+-------+
5 rows in set
自然连接
自然连接无需指定连接列,SQL会检查两个表中是否有相同名称的列,且相同的列名称只能有一个,自然连接基本不用。
mysql> SELECT * FROM student NATURAL JOIN class;
+----+------+-----+---------+-------+
| id | name | age | classId | cname |
+----+------+-----+---------+-------+
| 1 | s1 | 20 | 1 | 一班 |
| 2 | s2 | 22 | 1 | 二班 |
| 3 | s3 | 22 | 2 | |
+----+------+-----+---------+-------+
3 rows in set
可以看到只有一列id了,因为student与class两张表中相同的id列自动合并了,相当于内连接
SELECT * FROM student INNER JOIN class ON student.id = class.id
如果更改class表的cname字段名称为name会出现什么情况呢?
mysql> ALTER TABLE class CHANGE cname name VARCHAR(10);
Query OK, 3 rows affected
Records: 3 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM student NATURAL JOIN class; -- 因为两张表同名字段有两个,所有结果为空
Empty set
由于现在student与class表有两个同名的字段,所有结果为空
最新文章
- java notepad++
- PHP简单漂亮的分页类
- 严格遵守“第一级DOM”能够让你避免与兼容性有关的任何问题
- 微信公众平台Php版php开发(转)
- WAMP搭建
- IE中console的正确使用方法
- Java 8 正式发布,新特性全搜罗
- 修改范围PHP_INI_SYSTEM与PHP_INI_ALL的区别
- redis学习-day1
- 程序员,一起玩转GitHub版本控制,超简单入门教程 干货2
- Linux学习笔记6-Linux根目录下各个目录的作用
- dropdownlist控件的几个属性selectedIndex、selectedItem、selectedValue、selectedItem.Text、selectedItem.value的区别
- Implement custom foreach function in C#
- oracle_利用闪回功能恢复数据
- 解决URL中文乱码问题--对中文进行加密、解密处理
- hostent h_addr_list
- js与jQuery对象相互转换
- Leetcode_235_Lowest Common Ancestor of a Binary Search Tree
- CSS position(定位)属性
- 20181207_Second_小结
热门文章
- PowerShell工作流学习-3-挂起工作流
- 冲刺博客NO.9
- C# Autofac集成之Framework WebAPI
- 另一个画风的GSS1 - Can you answer these queries I(猫树)
- 基于vue的颜色选择器vue-color-picker
- a标签嵌套a标签在实际项目开发中遇到的坑
- idea : shorten command line
- IDEA Exception in thread ";main"; java.lang.ClassNotFoundException: com.streamax.servicecore.business.FileManageServApplication
- 829. 连续整数求和-leetcode
- ETCD&;Flannel安装