更多狀況下,咱們查詢的數據來源於多張表,全部有必要了解一下MySQL中的鏈接查詢。mysql
SQL中將鏈接查詢分紅四類:交叉鏈接,內鏈接,外鏈接和天然鏈接。sql
數據準備blog
student表ci
-- ---------------------------- -- 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表class
-- ---------------------------- -- 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條記錄。im
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
等價於di
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表有兩個同名的字段,全部結果爲空