,ON或USING條件的時候一般不建議使用,因為當數據表項目太多<" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

MySQL中的各種JOIN(CROSS JOIN, INNER JOIN, LE

系統 3227 0
MySQL中的各種JOIN

1. 笛卡爾積(交叉連接)
在MySQL中可以為CROSS JOIN或者省略CROSS即JOIN,或者使用','

SELECT * FROM table1 CROSS JOIN table2
SELECT * FROM table1 JOIN table2
SELECT * FROM table1,table2

由于其返回的結果為被連接的兩個數據表的乘積,因此當有WHERE<wbr></wbr>, ON或USING條件的時候一般不建議使用,因為當數據表項目太多<wbr></wbr>的時候,會非常慢。
一般使用LEFT [OUTER] JOIN或者RIGHT [OUTER] JOIN

2. 內連接INNER JOIN
在MySQL中把INNER JOIN叫做等值連接,即需要指定等值連接條件
在MySQL中CROSS和INNER JOIN被劃分在一起,不明白。
參看MySQL幫助手冊
http://dev.mysql.com/doc<wbr></wbr>/refman/5.0/en/join.html
join_table:
table_reference [INNER | CROSS] JOIN table_factor [join_condition]

3. MySQL中的外連接,分為左外連接和右連接,
即除了返回符合連接條件的結果之外,還要返回左表(左連接<wbr></wbr>)或者右表(右連接)中不符合連接條件的結果,相對應的使用NUL<wbr></wbr>L對應。

a. LEFT [OUTER] JOIN
SELECT column_name FROM table1 LEFT [OUTER] JOIN table2 ON table1.column=table2.column
除了返回符合連接條件的結果之外,還需要顯示左表中不符合連接條件<wbr></wbr>的數據列,相對應使用NULL對應

b. RIGHT [OUTER] JOIN
SELECT column_name FROM table1 RIGHT [OUTER] JOIN table2 ON table1.column=table2.column
RIGHT與LEFT JOIN相似不同的僅僅是除了顯示符合連接條件的結果之外<wbr></wbr>,還需要顯示右表中不符合連接條件的數據列,相應使用NULL對應


------------------------------<wbr></wbr>--------------
添加顯示條件WHERE, ON, USING
1. WHERE子句
2. ON
3. USING子句,如果連接的兩個表連接條件的兩個列具有相同的名字<wbr></wbr>的話可以使用USING
例如
SELECT <column_name> FROM <table1> LEFT JOIN <table2> USING (<column_name>)

連接多余兩個表的情況
舉例:
mysql> SELECT artists.Artist, cds.title, genres.genre
-> FROM cds
-> LEFT JOIN genres
-> ON cds.genreID = genres.genreID
-> LEFT JOIN artists
-> ON cds.artistID = artists.artistID;
或者
mysql> SELECT artists.Artist, cds.title, genres.genre
-> FROM cds
-> LEFT JOIN genres
-> ON cds.genreID = genres.genreID
-> LEFT JOIN artists
-> ON cds.artistID = artists.artistID
-> WHERE (genres.genre = 'Pop');
------------------------------<wbr></wbr>--------------

另外需要注意的地方

在MySQL中涉及到多表查詢的時候,需要根據查詢的情況<wbr></wbr>,想好使用哪種連接方式效率更高。
1. 交叉連接(笛卡爾積)或者內連接
[INNER | CROSS] JOIN
2. 左外連接LEFT [OUTER] JOIN或者右外連接RIGHT [OUTER] JOIN

注意指定連接條件WHERE, ON,USING.

------------------------------<wbr></wbr>--------------
看懂MySQL手冊定義的MySQL各種JOIN的用法:
//看懂如下的定義方式
                
                  
                    table_references:
                  
                
                
table_reference [,
table_reference
] ...

//不同的JOIN EXPRESSION之間使用','分割
A table reference is also known as a join expression.


table_reference
:
table_factor
| join_table


//每個JOIN EXPRESSION由數據表table_factor以及JOI<wbr></wbr>N表達式構成join_table


table_factor :

tbl_name
[[AS] alias ] [ index_hint )]
| ( table_references )
| { OJ
table_reference
LEFT OUTER JOIN table_reference
ON conditional_expr }


// 數據表table_factor,注意其遞歸定義的table<wbr></wbr>_references



join_table :
table_reference [INNER | CROSS] JOIN
table_factor [ join_condition ]
| table_reference STRAIGHT_JOIN
table_factor

| table_reference STRAIGHT_JOIN table_factor ON condition
|
table_reference LEFT [OUTER] JOIN table_reference join_condition
|
table_reference
NATURAL [LEFT [OUTER]] JOIN table_factor
| table_reference RIGHT [OUTER] JOIN
table_reference
join_condition
| table_reference NATURAL [RIGHT [OUTER]] JOIN table_factor


//數據表的連接表達式join_table

join_condition :
ON conditional_expr

| USING ( column_list )

//連接表達式的連接條件定義使用ON或者USING


index_hint :
USE {INDEX|KEY} [FOR JOIN] ( index_list
)
| IGNORE {INDEX|KEY} [FOR JOIN] ( index_list )
| FORCE {INDEX|KEY} [FOR JOIN] ( index_list )


index_list
:
index_name [, index_name ] ...

MySQL手冊中提到的JOIN需要注意的地方:

1.
In MySQL, CROSS JOIN is a syntactic equivalent to INNER JOIN (they can replace each other). In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise.
手冊中提到
標準SQL中CROSS JOIN交叉連接(笛卡爾積)和內連接INNER JOIN不同,但是MySQL中兩者是相同的,即有[CROSS | INNER] JOIN,兩者可以互相替代,而且可以只使用JOIN

2. A table reference can be aliased using tbl_name AS alias_name or tbl_name alias_name :
                SELECT 
                
                  t1.name
                
                , t2.salary
                
FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name ;
可以對數據表使用別名

3. ,運算符
例如
SELECT * FROM table1,table2
由于在MySQL中INNER JOIN與CROSS JOIN相同,INNER JOIN和 , 在MySQL也相同,都是產生兩個表的笛卡爾積Cartesian Product
(等于兩個表格的行數乘積)

但是,號的優先級要低于INNER JOIN, CROSS JOIN, LEFT JOIN

因此
If you mix comma joins with the other join types when there is a join condition, an error of the form Unknown column 'col_name' in 'on clause' may occur.

4. 什么時候使用ON,什么時候使用WHERE
ON應該用戶數據表連接的時候指定連接條件;

WHERE用于用戶限制所選取的列

例如ON a.column=b.column
WHERE a.column='hello'

5. 可以使用LEFT JOIN查看,兩個連接的表中,不符合連接條件的部分<wbr></wbr>,因為不符合條件的部分LEFT JOIN之后會顯示為NULL
If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table. You can use this fact to find rows in a table that have no counterpart in another table:


SELECT left_tbl.*
FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id
WHERE right_tbl.id IS NULL;

This example finds all rows in left_tbl with an id value that is not present in right_tbl (that is, all rows in left_tbl with no corresponding row in right_tbl). This assumes that right_tbl.id is declared NOT NULL.


6.
當別連接的表指定連接條件的列舉有相同的名稱的時候,不需要
ON a.column=b.column不同的時候才使用ON a.column_a=b.column_b
可以使用USING (column)
當然也可以使用多個USING (c1,c2,c3)

The USING(column_list) clause names a list of columns that must exist in both tables. If tables a and b both contain columns c1, c2, and c3, the following join compares corresponding columns from the two tables:


a LEFT JOIN b USING (c1,c2,c3)

7.
其他的:
#
The NATURAL [LEFT] JOIN of two tables is defined to be semantically equivalent to an INNER JOIN or a LEFT JOIN with a USING clause that names all columns that exist in both tables.

#
RIGHT JOIN works analogously to LEFT JOIN. To keep code portable across databases, it is recommended that you use LEFT JOIN instead of RIGHT JOIN.
#
The { OJ ... LEFT OUTER JOIN ...} syntax shown in the join syntax description exists only for compatibility with ODBC. The curly braces in the syntax should be written literally; they are not metasyntax as used elsewhere in syntax descriptions.

#
STRAIGHT_JOIN is similar to JOIN, except that the left table is always read before the right table. This can be used for those (few) cases for which the join optimizer puts the tables in the wrong order.



參考資料
http://www.w3schools.com/sql<wbr></wbr>/sql_join.asp
http://www.keithjbrown.co.uk<wbr></wbr>/vworks/mysql/mysql_p5.php
http://dev.mysql.com/doc<wbr></wbr>/refman/5.0/en/join.html

回復 轉發

MySQL中的各種JOIN(CROSS JOIN, INNER JOIN, LEFT [OUTER] JOIN)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产高清美女一级a毛片久久 | 亚洲欧美日韩一区二区在线观看 | 国产精品久久久久激情影院 | 亚洲一区二区三区视频 | 国产毛片a级| 神马我不卡在线观看 | 九九九九精品视频在线播放 | 国产香蕉一区二区精品视频 | 国产精品线在线精品国语 | 国产成+人+综合+亚洲专 | 欧美黑人激情性久久 | 色综合天天综久久久噜噜噜久久〔 | 成人a视频高清在线观看 | 免费又黄又爽视频 | 韩国高清乱理伦片中文 | 婷婷在线视频观看 | 婷婷色视频 | 天天干天天插天天 | 欧美日韩加勒比一区二区三区 | 久久日本精品99久久久 | 亚洲色中文字幕在线播放 | 欧美夜色 | 亚洲最新视频在线观看 | 中国特黄特级真人毛片 | 国产麻豆精品手机在线观看 | 思思久热re6这里有精品 | 久久久伊香蕉网站 | 久久99热这里只有精品7 | 国产永久免费爽视频在线 | 欧美午夜寂寞影院安卓列表 | 全部无卡免费的毛片在线看 | 国产亚洲精品久久久久久小说 | 伊人久久伊人 | 色综色| 国产91久久最新观看地址 | 亚洲视频毛片 | 操一操干一干 | 美女一区二区三区 | 97高清| 欧美精品午夜毛片免费看 | 日本人69视频页码jlzz |