文章摘要:
1、JOIN / INNER JOIN 内连接
2、LEFT JOIN 关键字 左连接
3、RIGHT JOIN 关键字 右连接
4、FULL JOIN 关键字 全连接
5、UNION 和 UNION ALL 操作符
ps:案例数据库数据:
mysql> select * from Persons;
+------+----------+-----------+----------------+----------+
| Id_P | LastName | FirstName | Address | City |
+------+----------+-----------+----------------+----------+
| 1 | Aaa | John | Oxford Street | London |
| 2 | Bush | George | Fifth Avenue | New York |
| 3 | Carter | Thomas | Changan Street | Beijing |
| 4 | Carter | George | xford Street | London |
| 6 | Gates | Bill | Xuanwumen 10 | Beijing |
| 9 | Wilson | Fred | Champs-Elysees | NULL |
+------+----------+-----------+----------------+----------+
6 rows in set (0.00 sec)
mysql> select * from Orders;
+------+---------+-------------+------+
| Id_O | Company | OrderNumber | Id_p |
+------+---------+-------------+------+
| 1 | IBM | 3532 | 1 |
| 2 | Baidu | 2356 | NULL |
| 3 | Ali | 6534 | 4 |
| 4 | Apple | 4698 | 2 |
| 5 | Tencent | 6953 | 5 |
| 6 | Xiaomi | 7890 | 4 |
| 7 | LeEco | 2876 | 9 |
| 8 | JD | 3847 | 6 |
+------+---------+-------------+------+
8 rows in set (0.00 sec)
1、JOIN / INNER JOIN 内连接
用于根据两个或多个表中的列之间的关系,从这些表中查询数据。
有时为了得到完整的结果,我们需要从两个或更多的表中获取结果。我们就需要执行 join。
数据库中的表可通过键将彼此联系起来。主键(Primary Key)是一个列,在这个列中的每一行的值都是唯一的。在表中,每个主键的值都是唯一的。这样做的目的是在不重复每个表中的所有数据的情况下,把表间的数据交叉捆绑在一起。
- 案例:如下两种方式 等同。
-- 联表查询
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNumber
FROM Persons, Orders
WHERE Persons.Id_P = Orders.Id_P;
-- 使用JOIN
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNumber
FROM Persons
JOIN Orders
ON Persons.Id_P = Orders.Id_P;
+----------+-----------+-------------+
| LastName | FirstName | OrderNumber |
+----------+-----------+-------------+
| Aaa | John | 3532 |
| Carter | George | 6534 |
| Bush | George | 4698 |
| Carter | George | 7890 |
| Wilson | Fred | 2876 |
| Gates | Bill | 3847 |
+----------+-----------+-------------+
6 rows in set (0.00 sec)
备注:关于JOIN
JOIN: 如果表中有至少一个匹配,则返回行
LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
FULL JOIN: 只要其中一个表中存在匹配,就返回行。
2、LEFT JOIN 关键字 左连接
LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。
- LEFT JOIN 关键字语法
SELECT column_name(s)FROM table_name1 LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name;
注释:在某些数据库中, LEFT JOIN 称为 LEFT OUTER JOIN。
- 案例
mysql> SELECT Persons.LastName, Persons.FirstName, Orders.OrderNumber
-> FROM Persons
-> LEFT JOIN Orders
-> ON Persons.Id_P=Orders.Id_P
-> ORDER BY Persons.LastName;
+----------+-----------+-------------+
| LastName | FirstName | OrderNumber |
+----------+-----------+-------------+
| Aaa | John | 3532 |
| Bush | George | 4698 |
| Carter | George | 6534 |
| Carter | George | 7890 |
| Carter | Thomas | NULL |
| Gates | Bill | 3847 |
| Wilson | Fred | 2876 |
+----------+-----------+-------------+
7 rows in set (0.00 sec)
3、RIGHT JOIN 关键字 右连接
RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行。
- RIGHT JOIN 关键字语法
SELECT column_name(s)FROM table_name1RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name;
注释:在某些数据库中, RIGHT JOIN 称为 RIGHT OUTER JOIN。
- 案例
mysql> SELECT Persons.LastName, Persons.FirstName, Orders.OrderNumber
-> FROM Persons
-> RIGHT JOIN Orders
-> ON Persons.Id_P=Orders.Id_P
-> ORDER BY Persons.LastName;
+----------+-----------+-------------+
| LastName | FirstName | OrderNumber |
+----------+-----------+-------------+
| NULL | NULL | 2356 |
| NULL | NULL | 6953 |
| Aaa | John | 3532 |
| Bush | George | 4698 |
| Carter | George | 6534 |
| Carter | George | 7890 |
| Gates | Bill | 3847 |
| Wilson | Fred | 2876 |
+----------+-----------+-------------+
8 rows in set (0.00 sec)
4、FULL JOIN 关键字 全连接
只要其中某个表存在匹配,FULL JOIN 关键字就会返回行。
- FULL JOIN 关键字语法
SELECT column_name(s)FROM table_name1FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name;
注释:在某些数据库中, FULL JOIN 称为 FULL OUTER JOIN。
- 注意:MySql不支持FULL JOIN ,执行FULL JOIN 等同于 JOIN。
5、UNION 和 UNION ALL 操作符
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。
- SQL UNION 语法:
SELECT column_name(s) FROM table_name1UNIONSELECT column_name(s) FROM table_name2
注释:默认地,UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL。
SELECT column_name(s) FROM table_name1UNION ALLSELECT column_name(s) FROM table_name2
另外,UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。
mysql> SELECT * FROM test.Persons where City like 'L%';
+------+----------+-----------+---------------+--------+
| Id_P | LastName | FirstName | Address | City |
+------+----------+-----------+---------------+--------+
| 1 | Aaa | John | Oxford Street | London |
| 4 | Carter | George | xford Street | London |
+------+----------+-----------+---------------+--------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM test.Persons where City regexp '^b';
+------+----------+-----------+----------------+---------+
| Id_P | LastName | FirstName | Address | City |
+------+----------+-----------+----------------+---------+
| 3 | Carter | Thomas | Changan Street | Beijing |
| 6 | Gates | Bill | Xuanwumen 10 | Beijing |
+------+----------+-----------+----------------+---------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM test.Persons where City like 'L%' union SELECT * FROM test.Persons where City regexp '^b';
+------+----------+-----------+----------------+---------+
| Id_P | LastName | FirstName | Address | City |
+------+----------+-----------+----------------+---------+
| 1 | Aaa | John | Oxford Street | London |
| 4 | Carter | George | xford Street | London |
| 3 | Carter | Thomas | Changan Street | Beijing |
| 6 | Gates | Bill | Xuanwumen 10 | Beijing |
+------+----------+-----------+----------------+---------+
4 rows in set (0.01 sec)
网友评论