mysql笔记-- mysql命令札记11 --MySql 连接

MySQL的联结(Join)语法

1.内联结、外联结、左联结、右联结的含义及区别:

 

在讲MySQL的Join语法前还是先回顾一下联结的语法,呵呵,其实连我自己都忘得差不多了,那就大家一起温习吧(如果内容有错误或有疑问,可以来信咨询:陈朋奕 chenpengyi#gmail.com),国内关于MySQL联结查询的资料十分少,相信大家在看了本文后会对MySQL联结语法有相当清晰的了解,也不会被Oracle的外联结的(“+”号)弄得糊涂了。

 

在SQL标准中规划的(Join)联结大致分为下面四种:

1.  内联结:将两个表中存在联结关系的字段符合联结关系的那些记录形成记录集的联结。

2.  外联结:分为外左联结和外右联结。

左联结A、B表的意思就是将表A中的全部记录和表B中联结的字段与表A的联结字段符合联结条件的那些记录形成的记录集的联结,这里注意的是最后出来的记录集会包括表A的全部记录。

右联结A、B表的结果和左联结B、A的结果是一样的,也就是说:

Select A.name B.name From A Left Join B On A.id=B.id

和Select A.name B.name From B Right Join A on B.id=A.id执行后的结果是一样的。

3.全联结:将两个表中存在联结关系的字段的所有记录取出形成记录集的联结(这个不需要记忆,只要是查询中提到了的表的字段都会取出,无论是否符合联结条件,因此意义不大)。

4.无联结:不用解释了吧,就是没有使用联结功能呗,也有自联结的说法。

 

这里我有个比较简便的记忆方法,内外联结的区别是内联结将去除所有不符合条件的记录,而外联结则保留其中部分。外左联结与外右联结的区别在于如果用A左联结B则A中所有记录都会保留在结果中,此时B中只有符合联结条件的记录,而右联结相反,这样也就不会混淆了。其实大家回忆高等教育出版社出版的《数据库系统概论》书中讲到关系代数那章(就是将笛卡儿积和投影那章)的内容,相信不难理解这些联结功能的内涵。

 

2.  MySQL联结(Join)的语法

 

MySQL支持Select和某些Update和Delete情况下的Join语法,具体语法上的细节有:

 

table_references:

    table_reference [, table_reference] …

 

table_reference:

    table_factor

  | join_table

 

table_factor:

    tbl_name [[AS] alias]

        [{USE|IGNORE|FORCE} INDEX (key_list)]

  | ( 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 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_condition:

    ON conditional_expr | USING (column_list)

 

上面的用法摘自权威资料,不过大家看了是否有点晕呢?呵呵,应该问题主要还在于table_reference是什么,table_factor又是什么?这里的table_reference其实就是表的引用的意思,因为在MySQL看来,联结就是一种对表的引用,因此把需要联结的表定义为table_reference,同时在SQL Standard中也是如此看待的。而table_factor则是MySQL对这个引用的功能上的增强和扩充,使得引用的表可以是括号内的一系列表,如下面例子中的JOIN后面括号:

 

SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)

 

这个语句的执行结果和下面语句其实是一样的:

 

SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4)

                 ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)

 

这两个例子不仅让我们了解了MySQL中table_factor和table_reference含义,同时能理解一点CROSS JOIN的用法,我要补充的是在MySQL现有版本中CROSS JOIN的作用和INNER JOIN是一样的(虽然在SQL Standard中是不一样的,然而在MySQL中他们的区别仅仅是INNER JOIN需要附加ON参数的语句,而CROSS JOIN不需要)。

既然说到了ON语句,那就解释一下吧,ON语句其实和WHERE语句功能大致相当,只是这里的ON语句是专门针对联结表的,ON语句后面的条件的要求和书写方式和WHERE语句的要求是一样的,大家基本上可以把ON当作WHERE用。

大家也许也看到了OJ table_reference LEFT OUTER JOIN table_reference这个句子,这不是MySQL的标准写法,只是为了和ODBC的SQL语法兼容而设定的,我很少用,Java的人更是不会用,所以也不多解释了。

那下面就具体讲讲简单的JOIN的用法了。首先我们假设有2个表A和B,他们的表结构和字段分别为:

 

表A:

ID

Name

1

Tim

2

Jimmy

3

John

4

Tom

表B:

ID

Hobby

1

Football

2

Basketball

2

Tennis

4

Soccer

 

1.  内联结:

Select A.Name B.Hobby from A, B where A.id = B.id,这是隐式的内联结,查询的结果是:

Name

Hobby

Tim

Football

Jimmy

Basketball

Jimmy

Tennis

Tom

Soccer

它的作用和 Select A.Name from A INNER JOIN B ON A.id = B.id是一样的。这里的INNER JOIN换成CROSS JOIN也是可以的。

2.  外左联结

Select A.Name from A Left JOIN B ON A.id = B.id,典型的外左联结,这样查询得到的结果将会是保留所有A表中联结字段的记录,若无与其相对应的B表中的字段记录则留空,结果如下:

Name

Hobby

Tim

Football

Jimmy

Basketball,Tennis

John

 

在查询中,连接的语法类似

[sql]  view plain copy
  1. SELECT select_expr FROM table_references  

table_references(对表的引用) 的定义如下(也可以看成 连接表达式 ):(晕晕晕哈)
[sql]  view plain copy
  1. table_references:  
  2.     table_reference [, table_reference] ...  
  3.   
  4. table_reference:  
  5.     table_factor  
  6.   | join_table  
  7.   
  8. table_factor:  
  9.     tbl_name [[AS] alias] [index_hint_list]  
  10.   | table_subquery [AS] alias  
  11.   | ( table_references )  
  12.   | { OJ table_reference LEFT OUTER JOIN table_reference  
  13.         ON conditional_expr }  
  14.   
  15. join_table:  
  16.     table_reference [INNER | CROSSJOIN table_factor [join_condition]  
  17.   | table_reference STRAIGHT_JOIN table_factor  
  18.   | table_reference STRAIGHT_JOIN table_factor ON conditional_expr  
  19.   | table_reference {LEFT|RIGHT} [OUTERJOIN table_reference join_condition  
  20.   | table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor  
  21.   
  22. join_condition:  
  23.     ON conditional_expr  
  24.   | USING (column_list)  
  25.   
  26. index_hint_list:  
  27.     index_hint [, index_hint] ...  
  28.   
  29. index_hint:  
  30.     USE {INDEX|KEY}  
  31.       [{FOR {JOIN|ORDER BY|GROUP BY}] ([index_list])  
  32.   | IGNORE {INDEX|KEY}  
  33.       [{FOR {JOIN|ORDER BY|GROUP BY}] (index_list)  
  34.   | FORCE {INDEX|KEY}  
  35.       [{FOR {JOIN|ORDER BY|GROUP BY}] (index_list)  
  36.   
  37. index_list:  
  38.     index_name [, index_name] ...  

其中,table_factor是基本的表选择,而join_table是基于表的一些扩展。
下面,通过实验介绍一下表连接。
首先,假设有以下几个表
table1
id book
1 java
2 c++
3 php

table2
id author
2 zhang
3 wang
4 li

table3
author year
zhang 2003
ma 2006
liu 2011

Inner Join 内连接
将两个表中存在连接关系的字段,组成的记录集,叫做内连接。
内连接等价于

[sql]  view plain copy
  1. mysql> select table1.id as id,book,author from table1, table2 where table1.id=table2.id;  
  2. +------+------+--------+  
  3. | id   | book | author |  
  4. +------+------+--------+  
  5. |    2 | c++  | zhang  |  
  6. |    3 | php  | wang   |  
  7. +------+------+--------+  
  8. rows in set (0.00 sec)  
  9. mysql> select * from table1 inner join table2 using (id);  
  10. +------+------+--------+  
  11. | id   | book | author |  
  12. +------+------+--------+  
  13. |    2 | c++  | zhang  |  
  14. |    3 | php  | wang   |  
  15. +------+------+--------+  
  16. 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是等价的。
[sql]  view plain copy
  1. mysql> select * from table1 inner join table2;  
  2. mysql> select * from table1 cross join table2;  
  3. mysql> select * from (table1, table2);  
  4. mysql> select * from table1 nature join table2;  
  5. 结果集:  
  6. +------+------+------+--------+  
  7. | id   | book | id   | author |  
  8. +------+------+------+--------+  
  9. |    1 | java |    2 | zhang  |  
  10. |    2 | c++  |    2 | zhang  |  
  11. |    3 | php  |    2 | zhang  |  
  12. |    1 | java |    3 | wang   |  
  13. |    2 | c++  |    3 | wang   |  
  14. |    3 | php  |    3 | wang   |  
  15. |    1 | java |    4 | li     |  
  16. |    2 | c++  |    4 | li     |  
  17. |    3 | php  |    4 | li     |  
  18. +------+------+------+--------+  

不难理解,下面两句SQL也是等价的。
[sql]  view plain copy
  1. mysql> select * from table1 left join (table2, table3) on (table2.id = table1.id and table2.author = table3.author);  
  2. mysql> select * from table1 left join (table2 cross join table3) on (table2.id = table1.id and table2.author = table3.author);  
  3. 结果集:  
  4. +------+------+------+--------+--------+------+  
  5. | id   | book | id   | author | author | year |  
  6. +------+------+------+--------+--------+------+  
  7. |    1 | java | NULL | NULL   | NULL   | NULL |  
  8. |    2 | c++  |    2 | zhang  | zhang  | 2003 |  
  9. |    3 | php  | NULL | NULL   | NULL   | NULL |  
  10. +------+------+------+--------+--------+------+  

Natural Join 自然连接
NATURAL [LEFT] JOIN:这个句子的作用相当于INNER JOIN,或者是在USING子句中包含了联结的表中所有公共字段的Left JOIN(左联结)。
也就是说:下面两个SQL是等价的。
[sql]  view plain copy
  1. mysql> select * from table1 natural join table2;  
  2. mysql> select * from table1 inner join table2 using (id);  
  3.   
  4. 结果集:  
  5. +------+------+--------+  
  6. | id   | book | author |  
  7. +------+------+--------+  
  8. |    2 | c++  | zhang  |  
  9. |    3 | php  | wang   |  
  10. +------+------+--------+  

同时,下面两个SQL也是等价的。
[sql]  view plain copy
  1. mysql> select * from table1 natural left join table2;  
  2. mysql> select * from table1 left join table2 using(id);  
  3. 结果集:  
  4. +------+------+--------+  
  5. | id   | book | author |  
  6. +------+------+--------+  
  7. |    1 | java | NULL   |  
  8. |    2 | c++  | zhang  |  
  9. |    3 | php  | wang   |  
  10. +------+------+--------+  

Left Join 左外连接
左外连接A、B表的意思就是将表A中的全部记录和表B中字段连接形成的记录集,这里注意的是最后出来的记录集会包括表A的全部记录。
左连接表1,表二等价于右连接表二,表一。如下两个SQL是等价的:
[sql]  view plain copy
  1. mysql> select * from table1 left join table2 using (id);  
  2. mysql> select * from table2 right join table1 using (id);  
  3. 结果集:  
  4. +------+------+--------+  
  5. | id   | book | author |  
  6. +------+------+--------+  
  7. |    1 | java | NULL   |  
  8. |    2 | c++  | zhang  |  
  9. |    3 | php  | wang   |  
  10. +------+------+--------+  

Right Join 右外连接

右外连接和左外连接是类似的。为了方便数据库便于访问,推荐使用左外连接代替右外连接。


最后,讲一下Mysql表连接的一些注意事项。

1、两个表求差集的方法
如果求 左表 - 右表 的差集,使用类似下面的SQL:
[sql]  view plain copy
  1. SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;  
  2. 例如  
  3. mysql> select table1.* from table1 left join table2 using(id) where table2.id is null;  
  4. +------+------+  
  5. | id   | book |  
  6. +------+------+  
  7. |    1 | java |  
  8. +------+------+  
  9. 1 row in set (0.00 sec)  

2、Using子句
Using子句可以使用On子句重写。但是使用Select * 查询出的结果有差别。以下两句话是等价的:
[sql]  view plain copy
  1. mysql> select id, book, author from table1 join table2 using (id);  
  2. mysql> select table1.id, book, author from table1 join table2 on table1.id=table2.id;  
  3. 结果集:  
  4. +------+------+--------+  
  5. | id   | book | author |  
  6. +------+------+--------+  
  7. |    2 | c++  | zhang  |  
  8. |    3 | php  | wang   |  
  9. +------+------+--------+  

但是下面两个有些许不同,使用on时候,重复的部分会被输出两次。
[sql]  view plain copy
  1. mysql> select * from table1 join table2 using (id);  
  2. +------+------+--------+  
  3. | id   | book | author |  
  4. +------+------+--------+  
  5. |    2 | c++  | zhang  |  
  6. |    3 | php  | wang   |  
  7. +------+------+--------+  
  8. rows in set (0.00 sec)  
  9. mysql> select * from table1 join table2 on table1.id=table2.id;  
  10. +------+------+------+--------+  
  11. | id   | book | id   | author |  
  12. +------+------+------+--------+  
  13. |    2 | c++  |    2 | zhang  |  
  14. |    3 | php  |    3 | wang   |  
  15. +------+------+------+--------+  
  16. 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。
[sql]  view plain copy
  1. SQL1 : SELECT * FROM t1, t2 JOIN t3 ON (t1.i1 = t3.i3);  
  2. SQL2 : SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3);  
  3. SQL3 : SELECT * FROM t1, (t2 JOIN t3 ON (t1.i1 = t3.i3));  

因此会报错,找不到i1列。因此以后在写这样的查询的时候,最好写明白,不要省略括号,这样能避免很多错误。

5、循环的自然连接
在MySQL 5.1版本中,SQL1等价于SQL3, 而在MySQL以前版本中,SQL1等价于SQL2。
[sql]  view plain copy
  1. SQL1 : SELECT ... FROM t1 NATURAL JOIN t2 NATURAL JOIN t3;  
  2. SQL2 : SELECT ... FROM t1, t2, t3 WHERE t1.b = t2.b AND t2.c = t3.c;  
  3. SQL3 : SELECT ... FROM t1, t2, t3 WHERE t1.b = t2.b AND t2.c = t3.c AND t1.a = t3.a;  

你可能感兴趣的:(mysql笔记-- mysql命令札记11 --MySql 连接)