设为首页 收藏本站
查看: 356|回复: 0

[经验分享] MySQL中的各种JOIN(CROSS JOIN, INNER JOIN, LEFT [OUTER] JOIN)

[复制链接]

尚未签到

发表于 2016-10-20 08:00:55 | 显示全部楼层 |阅读模式

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







http://mail.google.com/mail/images/reply.gif回复

http://mail.google.com/mail/images/forward.gif转发

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-288606-1-1.html 上篇帖子: 中文字符 Mysql Data too long for column 'name' at row 1 下篇帖子: Spring+Hibernate框架下Mysql读写分离、主从数据库配置(上)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表