在這種情況下,左連接與右連接相同?右連接與左連接
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>
[SQL的可視說明聯接](http://www.codinghorror.com/blog/ 2007/10/A-視覺解釋-的-SQL joins.html)。 – eggyal 2012-08-04 08:57:41
這意味着值(NEWSGROUP.ID)=值(ARTICLE.NEWSGROUP_ID) – 2012-08-04 09:08:02
我想爲您描述SQL連接 – 2015-05-14 08:34:23