以下的文章主要介紹的是MySQL連接用法總結,以及MySQL連接的概念,各種連接的具體使用方案,數據庫增量同步實例的介紹,如果你對這些相關的內容心存好奇的話,你就可以對以下的文章進行閱讀了。
1、MySQL連接簡介
MySQL支持的連接類型如下:
交叉連接、內連接、外連接左外MySQL連接和右外連接)、自連接、聯合
2、各種連接的使用方法
在演示各種MySQL連接的用法之前,我們先定義如下的數據庫表格,以後的演示就使用它們。
- mysql> select * from t_users;
- +---------+-----------+---------+---------------------+
- | iUserID | sUserName | iStatus | dtLastTime |
- +---------+-----------+---------+---------------------+
- | 1 | baidu | 0 | 2010-06-27 15:04:03 |
- | 2 | google | 0 | 2010-06-27 15:04:03 |
- | 3 | yahoo | 0 | 2010-06-27 15:04:03 |
- | 4 | tencent | 0 | 2010-06-27 15:04:03 |
- +---------+-----------+---------+---------------------+mysql> select * from t_groups;
- +----------+------------+---------------------+
- | iGroupID | sGroupName | dtLastTime |
- +----------+------------+---------------------+
- | 1 | spring | 2010-06-27 15:04:03 |
- | 2 | summer | 2010-06-27 15:04:03 |
- | 3 | autumn | 2010-06-27 15:04:03 |
- | 4 | winter | 2010-06-27 15:04:03 |
- +----------+------------+---------------------+mysql> select * from t_users_groups;
- +---------+----------+---------------------+
- | iUserID | iGroupID | dtLastTime |
- +---------+----------+---------------------+
- | 1 | 1 | 2010-06-27 15:04:03 |
- | 2 | 1 | 2010-06-27 15:04:03 |
- | 4 | 3 | 2010-06-27 15:04:03 |
- | 6 | 4 | 2010-06-27 15:04:03 |
- +---------+----------+---------------------+1.交叉連接
2.內連接
3.外連接
外連接有什麼特點?簡而言之,外連接作用在通過某個key相連接的兩張表上,它首先從A表中依次讀出每行數據,然後到與之相連接的B表,尋找具有相同key值的記錄。如果有匹配行,A和B的對應記錄組成新結果行;如果沒有,A與一條各字段為NULL的B記錄組成新結果行。
到底從哪個表中選擇所有行,SQL標准定義了左外連接和右外連接。
左外連接:
- mysql> SELECT * FROM t_users LEFT JOIN t_users_groups ON t_users.iUserID=t_users_groups.iUserID;
- +---------+-----------+---------+---------------------+---------+----------+---------------------+
- | iUserID | sUserName | iStatus | dtLastTime | iUserID | iGroupID | dtLastTime |
- +---------+-----------+---------+---------------------+---------+----------+---------------------+
- | 1 | baidu | 0 | 2010-06-27 15:04:03 | 1 | 1 | 2010-06-27 15:04:03 |
- | 2 | google | 1 | 2010-06-27 15:46:51 | 2 | 1 | 2010-06-27 15:04:03 |
- | 3 | yahoo | 1 | 2010-06-27 15:46:51 | NULL | NULL | NULL |
- | 4 | tencent | 0 | 2010-06-27 15:04:03 | 4 | 3 | 2010-06-27 15:04:03 |
- +---------+-----------+---------+---------------------+---------+----------+---------------------+
- 4 rows in set (0.00 sec)
t_users為上述描述中的A表,t_users_groups為B表。
右外連接:
- mysql> SELECT * FROM t_users RIGHT JOIN t_users_groups ON t_users.iUserID=t_users_groups.iUserID;
- +---------+-----------+---------+---------------------+---------+----------+---------------------+
- | iUserID | sUserName | iStatus | dtLastTime | iUserID | iGroupID | dtLastTime |
- +---------+-----------+---------+---------------------+---------+----------+---------------------+
- | 1 | baidu | 0 | 2010-06-27 15:04:03 | 1 | 1 | 2010-06-27 15:04:03 |
- | 2 | google | 1 | 2010-06-27 15:46:51 | 2 | 1 | 2010-06-27 15:04:03 |
- | 4 | tencent | 0 | 2010-06-27 15:04:03 | 4 | 3 | 2010-06-27 15:04:03 |
- | NULL | NULL | NULL | NULL | 6 | 4 | 2010-06-27 15:04:03 |
- +---------+-----------+---------+---------------------+---------+----------+---------------------+
- 4 rows in set (0.00 sec)
t_users_groups為上述描述中的A表,t_users為B表。
4.自MySQL連接
5.聯合
UNION運算符表示聯合,它用來把多個SELECT查詢的結果連接成一個單獨的結果集,但在MySQL連接時去除重復行。可以使用UNION連接盡可能多的SELECT查詢,但要謹記兩個基本條件。首先,每個SELECT查詢返回的字段個數必須相同。第二,每個SELECT查詢的字段類型必須依次相同。
我們舉個聯合例子:
- mysql> SELECT iUserID,sUserName,dtLastTime FROM t_users
- -> UNION
- -> SELECT iGroupID,sGroupName,dtLastTime FROM t_groups;
- +---------+-----------+---------------------+
- | iUserID | sUserName | dtLastTime |
- +---------+-----------+---------------------+
- | 1 | baidu | 2010-06-27 15:04:03 |
- | 2 | google | 2010-06-27 15:46:51 |
- | 3 | yahoo | 2010-06-27 15:46:51 |
- | 4 | tencent | 2010-06-27 15:04:03 |
- | 1 | spring | 2010-06-27 15:04:03 |
- | 2 | summer | 2010-06-27 15:04:03 |
- | 3 | autumn | 2010-06-27 15:04:03 |
- | 4 | winter | 2010-06-27 15:04:03 |
- +---------+-----------+---------------------+
8 rows in set (0.01 sec)
對UNION的每個SELECT添加ORDER BY子句是沒有意義的,如果要排序則必須將其施加到最後的結果集上。比如我們要對上面的例子中的iUserID進行排序,應該使用如下的SQL語句:
- mysql> (SELECT iUserID,sUserName,dtLastTime FROM t_users)
- -> UNION
- -> (SELECT iGroupID,sGroupName,dtLastTime FROM t_groups)
- -> ORDER BY iUserID ASC;
- +---------+-----------+---------------------+
- | iUserID | sUserName | dtLastTime |
- +---------+-----------+---------------------+
- | 1 | baidu | 2010-06-27 15:04:03 |
- | 1 | spring | 2010-06-27 15:04:03 |
- | 2 | google | 2010-06-27 15:46:51 |
- | 2 | summer | 2010-06-27 15:04:03 |
- | 3 | yahoo | 2010-06-27 15:46:51 |
- | 3 | autumn | 2010-06-27 15:04:03 |
- | 4 | tencent | 2010-06-27 15:04:03 |
- | 4 | winter | 2010-06-27 15:04:03 |
- +---------+-----------+---------------------+
- 8 rows in set (0.02 sec)
以上的相關內容就是對MySQL連接與各種連接的使用方法的介紹,望你能有所收獲。