mysql> SELECT * FROM Table_A ORDER BY PK ASC; +----+---------+ | PK | Value | +----+---------+ | 1 | both ab | | 2 | only a | +----+---------+ 2 rows in set (0.00 sec)
mysql> SELECT * from Table_B ORDER BY PK ASC; +----+---------+ | PK | Value | +----+---------+ | 1 | both ab | | 3 | only b | +----+---------+ 2 rows in set (0.00 sec)
SELECT A.PK AS A_PK, B.PK AS B_PK, A.Value AS A_Value, B.Value AS B_Value FROM Table_A A INNERJOIN Table_B B ON A.PK = B.PK;
查询结果:
1 2 3 4 5 6
+------+------+---------+---------+ | A_PK | B_PK | A_Value | B_Value | +------+------+---------+---------+ | 1 | 1 | both ab | both ab | +------+------+---------+---------+ 1 row in set (0.00 sec)
注:其中 A 为 Table_A 的别名,B 为 Table_B 的别名,下同。
LEFT JOIN
LEFT JOIN 一般被译作左连接,也写作 LEFT OUTER JOIN。左连接查询会返回左表(表 A)中所有记录,不管右表(表 B)中有没有关联的数据。在右表中找到的关联数据列也会被一起返回。
文氏图:
示例查询:
1 2 3 4 5
SELECT A.PK AS A_PK, B.PK AS B_PK, A.Value AS A_Value, B.Value AS B_Value FROM Table_A A LEFTJOIN Table_B B ON A.PK = B.PK;
查询结果:
1 2 3 4 5 6 7
+------+------+---------+---------+ | A_PK | B_PK | A_Value | B_Value | +------+------+---------+---------+ | 1 | 1 | both ab | both ba | | 2 | NULL | only a | NULL | +------+------+---------+---------+ 2 rows in set (0.00 sec)
RIGHT JOIN
RIGHT JOIN 一般被译作右连接,也写作 RIGHT OUTER JOIN。右连接查询会返回右表(表 B)中所有记录,不管左表(表 A)中有没有关联的数据。在左表中找到的关联数据列也会被一起返回。
文氏图:
示例查询:
1 2 3 4 5
SELECT A.PK AS A_PK, B.PK AS B_PK, A.Value AS A_Value, B.Value AS B_Value FROM Table_A A RIGHTJOIN Table_B B ON A.PK = B.PK;
查询结果:
1 2 3 4 5 6 7
+------+------+---------+---------+ | A_PK | B_PK | A_Value | B_Value | +------+------+---------+---------+ | 1 | 1 | both ab | both ba | | NULL | 3 | NULL | only b | +------+------+---------+---------+ 2 rows in set (0.00 sec)
FULL OUTER JOIN
FULL OUTER JOIN 一般被译作外连接、全连接,实际查询语句中可以写作 FULL OUTER JOIN 或 FULL JOIN。外连接查询能返回左右表里的所有记录,其中左右表里能关联起来的记录被连接后返回。
文氏图:
示例查询:
1 2 3 4 5
SELECT A.PK AS A_PK, B.PK AS B_PK, A.Value AS A_Value, B.Value AS B_Value FROM Table_A A FULLOUTERJOIN Table_B B ON A.PK = B.PK;
查询结果:
1 2
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FULL OUTER JOIN Table_B B ON A.PK = B.PK' at line 4
注:我当前示例使用的 MySQL 不支持 FULL OUTER JOIN。
应当返回的结果(使用 UNION 模拟):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
mysql> SELECT * -> FROM Table_A -> LEFT JOIN Table_B -> ON Table_A.PK = Table_B.PK -> UNION ALL -> SELECT * -> FROM Table_A -> RIGHT JOIN Table_B -> ON Table_A.PK = Table_B.PK -> WHERE Table_A.PK IS NULL; +------+---------+------+---------+ | PK | Value | PK | Value | +------+---------+------+---------+ | 1 | both ab | 1 | both ba | | 2 | only a | NULL | NULL | | NULL | NULL | 3 | only b | +------+---------+------+---------+ 3 rows in set (0.00 sec)
小结
以上四种,就是 SQL 里常见 JOIN 的种类和概念了,看一下它们的合影:
有没有感觉少了些什么,学数学集合时完全不止这几种情况?确实如此,继续看。
延伸用法
LEFT JOIN EXCLUDING INNER JOIN
返回左表有但右表没有关联数据的记录集。
文氏图:
示例查询:
1 2 3 4 5 6
SELECT A.PK AS A_PK, B.PK AS B_PK, A.Value AS A_Value, B.Value AS B_Value FROM Table_A A LEFTJOIN Table_B B ON A.PK = B.PK WHERE B.PK ISNULL;
查询结果:
1 2 3 4 5 6
+------+------+---------+---------+ | A_PK | B_PK | A_Value | B_Value | +------+------+---------+---------+ | 2 | NULL | only a | NULL | +------+------+---------+---------+ 1 row in set (0.01 sec)
RIGHT JOIN EXCLUDING INNER JOIN
返回右表有但左表没有关联数据的记录集。
文氏图:
示例查询:
1 2 3 4 5 6
SELECT A.PK AS A_PK, B.PK AS B_PK, A.Value AS A_Value, B.Value AS B_Value FROM Table_A A RIGHTJOIN Table_B B ON A.PK = B.PK WHERE A.PK ISNULL;
查询结果:
1 2 3 4 5 6
+------+------+---------+---------+ | A_PK | B_PK | A_Value | B_Value | +------+------+---------+---------+ | NULL | 3 | NULL | only b | +------+------+---------+---------+ 1 row in set (0.00 sec)
FULL OUTER JOIN EXCLUDING INNER JOIN
返回左表和右表里没有相互关联的记录集。
文氏图:
示例查询:
1 2 3 4 5 6 7
SELECT A.PK AS A_PK, B.PK AS B_PK, A.Value AS A_Value, B.Value AS B_Value FROM Table_A A FULLOUTERJOIN Table_B B ON A.PK = B.PK WHERE A.PK ISNULL OR B.PK ISNULL;
因为使用到了 FULL OUTER JOIN,MySQL 在执行该查询时再次报错。
1 2 3 4
ERROR1064 (42000): You have an errorin your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax touse near 'FULL OUTER JOIN Table_B B ON A.PK = B.PK WHERE A.PK ISNULL OR B.PK ISNULL' at line4
应当返回的结果(用 UNION 模拟):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
mysql> SELECT * -> FROM Table_A -> LEFT JOIN Table_B -> ON Table_A.PK = Table_B.PK -> WHERE Table_B.PK IS NULL -> UNION ALL -> SELECT * -> FROM Table_A -> RIGHT JOIN Table_B -> ON Table_A.PK = Table_B.PK -> WHERE Table_A.PK IS NULL; +------+--------+------+--------+ | PK | Value | PK | Value | +------+--------+------+--------+ | 2 | only a | NULL | NULL | | NULL | NULL | 3 | only b | +------+--------+------+--------+ 2 rows in set (0.00 sec)
SELECT A.PK AS A_PK, B.PK AS B_PK, A.Value AS A_Value, B.Value AS B_Value FROM Table_A A CROSSJOIN Table_B B;
查询结果:
1 2 3 4 5 6 7 8 9
+------+------+---------+---------+ | A_PK | B_PK | A_Value | B_Value | +------+------+---------+---------+ | 1 | 1 | both ab | both ba | | 2 | 1 | only a | both ba | | 1 | 3 | both ab | only b | | 2 | 3 | only a | only b | +------+------+---------+---------+ 4 rows in set (0.00 sec)
SELECT A.EMP_ID AS EMP_ID, A.EMP_NAME AS EMP_NAME, B.EMP_ID AS EMP_SUPV_ID, B.EMP_NAME AS EMP_SUPV_NAME FROM Table_C A, Table_C B WHERE A.EMP_SUPV_ID = B.EMP_ID;
查询结果:
1 2 3 4 5 6
+--------+----------+-------------+---------------+ | EMP_ID | EMP_NAME | EMP_SUPV_ID | EMP_SUPV_NAME | +--------+----------+-------------+---------------+ | 1002 | Zhuang | 1001 | Ma | +--------+----------+-------------+---------------+ 1 row in set (0.00 sec)
补充说明
文中的图使用 Keynote 绘制;
个人的体会是 SQL 里的 JOIN 查询与数学里的求交集、并集等很像;
SQLite 不支持 RIGHT JOIN 和 FULL OUTER JOIN,可以使用 LEFT JOIN 和 UNION 来达到相同的效果;
MySQL 不支持 FULL OUTER JOIN,可以使用 LEFT JOIN 和 UNION 来达到相同的效果;