在MySQL(以5.1為例)中,表格連接的語法可以參考MySQL官方手冊:MySQL官方手冊-JOIN
在查詢中,連接的語法類似
SELECT select_expr FROM table_references
#table_references(對錶的引用)的定義如下(也可以看成連接表達式#):(暈暈暈暈哈)
table_references: table_reference [, table_reference] ... table_reference: table_factor | join_table table_factor: tbl_name [[AS] alias] [index_hint_list] | table_subquery [AS] alias | ( table_references ) | { OJ table_reference LEFT OUTER JOIN table_reference ON conditional_expr } join_table: table_reference [INNER | CROSS] JOIN table_factor [join_condition] | table_reference STRAIGHT_JOIN table_factor | table_reference STRAIGHT_JOIN table_factor ON conditional_expr | table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition | table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor join_condition: ON conditional_expr | USING (column_list) index_hint_list: index_hint [, index_hint] ... index_hint: USE {INDEX|KEY} [{FOR {JOIN|ORDER BY|GROUP BY}] ([index_list]) | IGNORE {INDEX|KEY} [{FOR {JOIN|ORDER BY|GROUP BY}] (index_list) | FORCE {INDEX|KEY} [{FOR {JOIN|ORDER BY|GROUP BY}] (index_list) index_list: index_name [, index_name] ...
其中,table_factor是基本的表格選擇,而join_table是基於表格的一些擴充。
下面,透過實驗介紹一下表連接。
首先,假設有以下幾個表格
#id | ##書|
---|---|
1 | |
#2 | |
zhang | |
wang |
##zhang | |
ma | |
##liu |
Inner Join 内连接
将两个表中存在连接关系的字段,组成的记录集,叫做内连接。
内连接等价于
mysql> select table1.id as id,book,author from table1, table2 where table1.id=table2.id; +------+------+--------+ | id | book | author | +------+------+--------+ | 2 | c++ | zhang | | 3 | php | wang | +------+------+--------+ 2 rows in set (0.00 sec) mysql> select * from table1 inner join table2 using (id); +------+------+--------+ | id | book | author | +------+------+--------+ | 2 | c++ | zhang | | 3 | php | wang | +------+------+--------+ 2 rows in set (0.00 sec)
可以看出,两者是等价的。没有Using子句的Inner Join相当于是求两个table的笛卡尔积。
Cross Join 交叉连接
在Mysql中,Cross Join可以用逗号表达式表示,例如(table1, table 2)。在Mysql中,Cross Join 和 Inner Join 是等价的,但是在标准SQL中,它们并不等价,Inner Join 用于带有on表达式的连接,反之用Cross Join。以下两个SQL语句是等价的。
Cross Join 指的是两个table的笛卡尔积。以下三句SQL是等价的。
mysql> select * from table1 inner join table2; mysql> select * from table1 cross join table2; mysql> select * from (table1, table2); mysql> select * from table1 nature join table2; 结果集: +------+------+------+--------+ | id | book | id | author | +------+------+------+--------+ | 1 | java | 2 | zhang | | 2 | c++ | 2 | zhang | | 3 | php | 2 | zhang | | 1 | java | 3 | wang | | 2 | c++ | 3 | wang | | 3 | php | 3 | wang | | 1 | java | 4 | li | | 2 | c++ | 4 | li | | 3 | php | 4 | li | +------+------+------+--------+
不难理解,下面两句SQL也是等价的。
mysql> select * from table1 left join (table2, table3) on (table2.id = table1.id and table2.author = table3.author); mysql> select * from table1 left join (table2 cross join table3) on (table2.id = table1.id and table2.author = table3.author); 结果集: +------+------+------+--------+--------+------+ | id | book | id | author | author | year | +------+------+------+--------+--------+------+ | 1 | java | NULL | NULL | NULL | NULL | | 2 | c++ | 2 | zhang | zhang | 2003 | | 3 | php | NULL | NULL | NULL | NULL | +------+------+------+--------+--------+------+
Natural Join 自然连接
NATURAL [LEFT] JOIN:这个句子的作用相当于INNER JOIN,或者是在USING子句中包含了联结的表中所有公共字段的Left JOIN(左联结)。
也就是说:下面两个SQL是等价的。
mysql> select * from table1 natural join table2; mysql> select * from table1 inner join table2 using (id); 结果集: +------+------+--------+ | id | book | author | +------+------+--------+ | 2 | c++ | zhang | | 3 | php | wang | +------+------+--------+
同时,下面两个SQL也是等价的。
mysql> select * from table1 natural left join table2; mysql> select * from table1 left join table2 using(id); 结果集: +------+------+--------+ | id | book | author | +------+------+--------+ | 1 | java | NULL | | 2 | c++ | zhang | | 3 | php | wang | +------+------+--------+
Left Join 左外连接
左外连接A、B表的意思就是将表A中的全部记录和表B中字段连接形成的记录集,这里注意的是最后出来的记录集会包括表A的全部记录。
左连接表1,表二等价于右连接表二,表一。如下两个SQL是等价的:
mysql> select * from table1 left join table2 using (id); mysql> select * from table2 right join table1 using (id); 结果集: +------+------+--------+ | id | book | author | +------+------+--------+ | 1 | java | NULL | | 2 | c++ | zhang | | 3 | php | wang | +------+------+--------+
Right Join 右外连接
右外连接和左外连接是类似的。为了方便数据库便于访问,推荐使用左外连接代替右外连接。
最后,讲一下Mysql表连接的一些注意事项。
1、两个表求差集的方法
如果求 左表 - 右表 的差集,使用类似下面的SQL:
SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL; 例如 mysql> select table1.* from table1 left join table2 using(id) where table2.id is null; +------+------+ | id | book | +------+------+ | 1 | java | +------+------+ 1 row in set (0.00 sec)
2、Using子句
Using子句可以使用On子句重写。但是使用Select * 查询出的结果有差别。以下两句话是等价的:
mysql> select id, book, author from table1 join table2 using (id); mysql> select table1.id, book, author from table1 join table2 on table1.id=table2.id; 结果集: +------+------+--------+ | id | book | author | +------+------+--------+ | 2 | c++ | zhang | | 3 | php | wang | +------+------+--------+
但是下面两个有些许不同,使用on时候,重复的部分会被输出两次。
mysql> select * from table1 join table2 using (id); +------+------+--------+ | id | book | author | +------+------+--------+ | 2 | c++ | zhang | | 3 | php | wang | +------+------+--------+ 2 rows in set (0.00 sec) mysql> select * from table1 join table2 on table1.id=table2.id; +------+------+------+--------+ | id | book | id | author | +------+------+------+--------+ | 2 | c++ | 2 | zhang | | 3 | php | 3 | wang | +------+------+------+--------+ 2 rows in set (0.00 sec)
3、Straight Join的使用
STRAIGHT_JOIN 和 JOIN相似,除了大部分情况下,在使用STRAIGHT_JOIN时候,先读右表后读左表。而在大部分情况下是先读左表的。STRAIGHT_JOIN仅用于少数情况下的表连接性能优化,比如右表记录数目明显少于左表。
4、Mysql表连接的运算顺序
在MySQL 5.1版本中,INNER JOIN, CROSS JOIN, LEFT JOIN, 和RIGHT JOIN 比逗号表达式具有更高的优先级。
因此SQL1被解析成SQL3,而不是SQL2。
SQL1 : SELECT * FROM t1, t2 JOIN t3 ON (t1.i1 = t3.i3); SQL2 : SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3); SQL3 : SELECT * FROM t1, (t2 JOIN t3 ON (t1.i1 = t3.i3));
因此会报错,找不到i1列。因此以后在写这样的查询的时候,最好写明白,不要省略括号,这样能避免很多错误。
5、循环的自然连接
在MySQL 5.1版本中,SQL1等价于SQL3, 而在MySQL以前版本中,SQL1等价于SQL2。
SQL1 : SELECT ... FROM t1 NATURAL JOIN t2 NATURAL JOIN t3; SQL2 : SELECT ... FROM t1, t2, t3 WHERE t1.b = t2.b AND t2.c = t3.c; SQL3 : SELECT ... FROM t1, t2, t3 WHERE t1.b = t2.b AND t2.c = t3.c AND t1.a = t3.a;
以上是MySQL中關於Join的使用範例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

存儲過程是MySQL中的預編譯SQL語句集合,用於提高性能和簡化複雜操作。 1.提高性能:首次編譯後,後續調用無需重新編譯。 2.提高安全性:通過權限控制限制數據表訪問。 3.簡化複雜操作:將多條SQL語句組合,簡化應用層邏輯。

MySQL查詢緩存的工作原理是通過存儲SELECT查詢的結果,當相同查詢再次執行時,直接返回緩存結果。 1)查詢緩存提高數據庫讀取性能,通過哈希值查找緩存結果。 2)配置簡單,在MySQL配置文件中設置query_cache_type和query_cache_size。 3)使用SQL_NO_CACHE關鍵字可以禁用特定查詢的緩存。 4)在高頻更新環境中,查詢緩存可能導致性能瓶頸,需通過監控和調整參數優化使用。

MySQL被廣泛應用於各種項目中的原因包括:1.高性能與可擴展性,支持多種存儲引擎;2.易於使用和維護,配置簡單且工具豐富;3.豐富的生態系統,吸引大量社區和第三方工具支持;4.跨平台支持,適用於多種操作系統。

MySQL數據庫升級的步驟包括:1.備份數據庫,2.停止當前MySQL服務,3.安裝新版本MySQL,4.啟動新版本MySQL服務,5.恢復數據庫。升級過程需注意兼容性問題,並可使用高級工具如PerconaToolkit進行測試和優化。

MySQL備份策略包括邏輯備份、物理備份、增量備份、基於復制的備份和雲備份。 1.邏輯備份使用mysqldump導出數據庫結構和數據,適合小型數據庫和版本遷移。 2.物理備份通過複製數據文件,速度快且全面,但需數據庫一致性。 3.增量備份利用二進制日誌記錄變化,適用於大型數據庫。 4.基於復制的備份通過從服務器備份,減少對生產系統的影響。 5.雲備份如AmazonRDS提供自動化解決方案,但成本和控制需考慮。選擇策略時應考慮數據庫大小、停機容忍度、恢復時間和恢復點目標。

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

在MySQL中優化數據庫模式設計可通過以下步驟提升性能:1.索引優化:在常用查詢列上創建索引,平衡查詢和插入更新的開銷。 2.表結構優化:通過規範化或反規範化減少數據冗餘,提高訪問效率。 3.數據類型選擇:使用合適的數據類型,如INT替代VARCHAR,減少存儲空間。 4.分區和分錶:對於大數據量,使用分區和分錶分散數據,提升查詢和維護效率。

tooptimizemysqlperformance,lofterTheSeSteps:1)inasemproperIndexingTospeedUpqueries,2)使用ExplaintplaintoAnalyzeandoptimizequeryPerformance,3)ActiveServerConfigurationStersLikeTlikeTlikeTlikeIkeLikeIkeIkeLikeIkeLikeIkeLikeIkeLikeNodb_buffer_pool_sizizeandmax_connections,4)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver CS6
視覺化網頁開發工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版