右连接与左连接

Posted

技术标签:

【中文标题】右连接与左连接【英文标题】:right join versus left join 【发布时间】:2012-08-04 08:49:17 【问题描述】:

在这种情况下,左连接与右连接相同吗?

mysql> 
mysql> 
mysql> use usenet;show tables;describe ARTICLE;describe NEWSGROUP;
Database changed
+------------------+
| Tables_in_usenet |
+------------------+
| ARTICLE          |
| NEWSGROUP        |
+------------------+
2 rows in set (0.00 sec)

+---------------+------------+------+-----+---------+----------------+
| Field         | Type       | Null | Key | Default | Extra          |
+---------------+------------+------+-----+---------+----------------+
| ID            | bigint(20) | NO   | PRI | NULL    | auto_increment |
| MESSAGENUMBER | int(11)    | YES  |     | NULL    |                |
| NEWSGROUP_ID  | bigint(20) | YES  | MUL | NULL    |                |
+---------------+------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| ID        | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| NEWSGROUP | varchar(255) | YES  |     | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> 
mysql> select * from ARTICLE right join NEWSGROUP on ARTICLE.NEWSGROUP_ID=NEWSGROUP.ID;
+------+---------------+--------------+----+-------------------------------+
| ID   | MESSAGENUMBER | NEWSGROUP_ID | ID | NEWSGROUP                     |
+------+---------------+--------------+----+-------------------------------+
|    1 |             4 |            1 |  1 | gwene.com.androidcentral      |
|    2 |             5 |            1 |  1 | gwene.com.androidcentral      |
|    3 |             6 |            1 |  1 | gwene.com.androidcentral      |
|    4 |             7 |            1 |  1 | gwene.com.androidcentral      |
|    5 |             8 |            1 |  1 | gwene.com.androidcentral      |
|    6 |             9 |            1 |  1 | gwene.com.androidcentral      |
|    7 |            10 |            1 |  1 | gwene.com.androidcentral      |
|    8 |            11 |            1 |  1 | gwene.com.androidcentral      |
|    9 |             4 |            2 |  2 | gwene.com.blogspot.emacsworld |
|   10 |             4 |            3 |  3 | gwene.com.blogspot.googlecode |
|   11 |             5 |            3 |  3 | gwene.com.blogspot.googlecode |
|   12 |             6 |            3 |  3 | gwene.com.blogspot.googlecode |
|   13 |             7 |            3 |  3 | gwene.com.blogspot.googlecode |
|   14 |             8 |            3 |  3 | gwene.com.blogspot.googlecode |
|   15 |             9 |            3 |  3 | gwene.com.blogspot.googlecode |
|   16 |            10 |            3 |  3 | gwene.com.blogspot.googlecode |
|   17 |            11 |            3 |  3 | gwene.com.blogspot.googlecode |
|   18 |             4 |            4 |  4 | gwene.com.economist           |
|   19 |             5 |            4 |  4 | gwene.com.economist           |
|   20 |             6 |            4 |  4 | gwene.com.economist           |
|   21 |             7 |            4 |  4 | gwene.com.economist           |
|   22 |             8 |            4 |  4 | gwene.com.economist           |
|   23 |             9 |            4 |  4 | gwene.com.economist           |
|   24 |            10 |            4 |  4 | gwene.com.economist           |
|   25 |            11 |            4 |  4 | gwene.com.economist           |
+------+---------------+--------------+----+-------------------------------+
25 rows in set (0.00 sec)

mysql> 
mysql> select * from ARTICLE left join NEWSGROUP on ARTICLE.NEWSGROUP_ID=NEWSGROUP.ID;
+----+---------------+--------------+------+-------------------------------+
| ID | MESSAGENUMBER | NEWSGROUP_ID | ID   | NEWSGROUP                     |
+----+---------------+--------------+------+-------------------------------+
|  1 |             4 |            1 |    1 | gwene.com.androidcentral      |
|  2 |             5 |            1 |    1 | gwene.com.androidcentral      |
|  3 |             6 |            1 |    1 | gwene.com.androidcentral      |
|  4 |             7 |            1 |    1 | gwene.com.androidcentral      |
|  5 |             8 |            1 |    1 | gwene.com.androidcentral      |
|  6 |             9 |            1 |    1 | gwene.com.androidcentral      |
|  7 |            10 |            1 |    1 | gwene.com.androidcentral      |
|  8 |            11 |            1 |    1 | gwene.com.androidcentral      |
|  9 |             4 |            2 |    2 | gwene.com.blogspot.emacsworld |
| 10 |             4 |            3 |    3 | gwene.com.blogspot.googlecode |
| 11 |             5 |            3 |    3 | gwene.com.blogspot.googlecode |
| 12 |             6 |            3 |    3 | gwene.com.blogspot.googlecode |
| 13 |             7 |            3 |    3 | gwene.com.blogspot.googlecode |
| 14 |             8 |            3 |    3 | gwene.com.blogspot.googlecode |
| 15 |             9 |            3 |    3 | gwene.com.blogspot.googlecode |
| 16 |            10 |            3 |    3 | gwene.com.blogspot.googlecode |
| 17 |            11 |            3 |    3 | gwene.com.blogspot.googlecode |
| 18 |             4 |            4 |    4 | gwene.com.economist           |
| 19 |             5 |            4 |    4 | gwene.com.economist           |
| 20 |             6 |            4 |    4 | gwene.com.economist           |
| 21 |             7 |            4 |    4 | gwene.com.economist           |
| 22 |             8 |            4 |    4 | gwene.com.economist           |
| 23 |             9 |            4 |    4 | gwene.com.economist           |
| 24 |            10 |            4 |    4 | gwene.com.economist           |
| 25 |            11 |            4 |    4 | gwene.com.economist           |
+----+---------------+--------------+------+-------------------------------+
25 rows in set (0.00 sec)

mysql> 
mysql> 

【问题讨论】:

A Visual Explanation of SQL Joins. 这意味着 values(NEWSGROUP.ID) = value(ARTICLE.NEWSGROUP_ID) 我想为你描述一下 SQL 连接 Difference between left join and right join in SQL Server的可能重复 【参考方案1】:

并非如此,因为 RIGHT JOIN 和 LEFT JOIN 是对称的。那就是:

A LEFT JOIN B = B RIGHT JOIN A

RIGHT JOIN 只是语法糖。优化器可以将右连接重写为左连接:

mysql> explain extended select * from t right join t t2 using (c1)\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t2
         type: index
possible_keys: NULL
          key: c2
      key_len: 5
          ref: NULL
         rows: 4201
     filtered: 100.00
        Extra: Using index
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: t
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: test.t2.c1
         rows: 1
     filtered: 100.00
        Extra: 
2 rows in set, 1 warning (0.00 sec)

注意优化器重写中的 LEFT JOIN(表已交换):

mysql> show warnings\G
*************************** 1. row ***************************
  Level: Note
   Code: 1003
Message: select `test`.`t2`.`c1` AS `c1`,`test`.`t2`.`c2` AS `c2`,`test`.`t`.`c2` AS
`c2` from `test`.`t` `t2` left join `test`.`t` on((`test`.`t`.`c1` = `test`.`t2`.`c1`))  where 1
1 row in set (0.00 sec) 

注意 (A RIGHT JOIN B != A LEFT JOIN B) 除非 (A INNER JOIN B = A LEFT JOIN B)。这是因为 A RIGHT JOIN B 与 A LEFT JOIN B 不对称(与 B LEFT JOIN A 对称)。

在您的情况下,A RIGHT JOIN B 将与 A LEFT JOIN B 相同,除非您要连接的列中有 NULL 值。如果有 NULL 值,则 A LEFT JOIN B 将与 A RIGHT JOIN B 不同。如果您添加新文章而不添加相关新闻组(反之亦然),那么结果也会发生变化。

【讨论】:

【参考方案2】:

Codeproject 有这张图片,它解释了 SQL 连接的简单基础知识,取自:http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx SQL 连接解释

【讨论】:

【参考方案3】:

使用您当前的数据,是的,它们是相同的。但是,由于 NEWSGROUP_ID 可以为空,因此它们可能会随着数据的变化而不同。

如果可能,我个人总是使用 LEFT JOIN(从主表到子表),事实上,在超过 6 年的 SQL 开发中,我只需要使用几次 RIGHT JOIN!

【讨论】:

以上是关于右连接与左连接的主要内容,如果未能解决你的问题,请参考以下文章

内连接与左外链接的区别

左外连接与右外连接区别?

SQL——左连接(Left join)右连接(Right join)内连接(Inner join)

sql中的inner join ,left join ,right join

sql四种连接方式

mysql连接查询