MySQL:连接类型的快速细分

MySQL: Quick breakdown of the types of joins

我想快速分解MySQL连接的类型。我知道这些,其余的我不知道它们是什么意思。

  • 逗号分隔(这到底是什么意思?):SELECT * FROM a, b WHERE b.id = a.beeId AND ...
  • 显示来自a的信息,即使b:SELECT * FROM a LEFT OUTER JOIN b ON b.id = a.beeId WHERE ...中没有匹配项

我见过其他连接,但想知道是什么使它们不同,添加INNEROUTER是什么改变了事情。

我已经知道连接是如何工作的,我只想知道是否还有其他类型的连接,或者它们只是获得相同结果的不同方法。


Found on G+(c)"在线可视化,发现G +日期"P></

see the following links or for a good的概述:P></

http:/ / / / /的index.php档案博客www.khankennels.com/2007/04/20/让我joins /P></

http:/ / / / / www.codinghorror.com博客2007年10 a-visual-explanation-of-sql-joins.htmlP></


基于你的评论,简单的定义是最好的发现在W3Schools of eacheach of the first在线简明explanation of the给A型和型P></

  • JOIN: Return rows when there is at least one match in both tables
  • LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
  • RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
  • FULL JOIN: Return rows when there is a match in one of the tables

前端编辑P></

在nutshell,the example of拆款给你P></

1
SELECT * FROM a, b WHERE b.id = a.beeId AND ...

选择的每一个记录表是从B和切割commas with the can be used the tables,这样也在列P></

1
SELECT a.beeName,b.* FROM a, b WHERE b.id = a.beeId AND ...

恩,让我instructed is the information in the row where the column have a column和a.beeid b.id比赛在你的实例。我知道你会得到它的example from a和b表信息a.beeid where the b.id equals。我会得到它的example of the information from the table和B a.beename column from the only when the information the a.beeid b.id equals。注that there is also an和条款,这将帮助你的refine结果。P></

有些无知的tutorials和explanations在线joins左joins MySQL和MySQL tizag' s have a look at tutorials。You can also基思J S退房。brown'网站信息在线joins that is for黑莓也那么好。P></

希望你helps thisP></


全外连接MySQL中不存在,你可以用need to left和right join 1例感染。P></


2:我有这样的P></

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> SELECT * FROM table_a;
+------+------+
| id   | name |
+------+------+
|    1 | row1 |
|    2 | row2 |
+------+------+

> SELECT * FROM table_b;
+------+------+------+
| id   | name | aid  |
+------+------+------+
|    3 | row3 |    1 |
|    4 | row4 |    1 |
|    5 | row5 | NULL |
+------+------+------+

内部连接的两表关心。P></

关于两表内加入的关心,如果你只得到一个两行的表。if there is超过一对多匹配,你就行。P></

1
2
3
4
5
6
7
> SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | id   | name | aid  |
+------+------+------+------+------+
|    1 | row1 |    3 | row3 | 1    |
|    1 | row1 |    4 | row4 | 1    |
+------+------+------+------+------+

它使不差to join the如果你逆向内秩序,因为它关心的:both aboutP></

1
2
3
4
5
6
7
> SELECT * FROM table_b b INNER JOIN table_a a ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | aid  | id   | name |
+------+------+------+------+------+
|    3 | row3 | 1    |    1 | row1 |
|    4 | row4 | 1    |    1 | row1 |
+------+------+------+------+------+

You get the same but the columns are rows,因为我们在不同的阶阶在上述不同的茶。P></

left join table about the第一只关心P></

about the first left join table的关心你给它,和不在乎多about the second,你总是知道get the rows if from the table是第一,there is the second:在对应的行不P></

1
2
3
4
5
6
7
8
> SELECT * FROM table_a a LEFT JOIN table_b b ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | id   | name | aid  |
+------+------+------+------+------+
|    1 | row1 |    3 | row3 | 1    |
|    1 | row1 |    4 | row4 | 1    |
|    2 | row2 | NULL | NULL | NULL |
+------+------+------+------+------+

你可以看到在上面的表_ rows of some of them a即使在table with do not match anything but not B,在B - _ rows of ones that table table的东西只_在比赛中。P></

if we the Order of the tables逆向,left join behaves differently:P></

1
2
3
4
5
6
7
8
> SELECT * FROM table_b b LEFT JOIN table_a a ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | aid  | id   | name |
+------+------+------+------+------+
|    3 | row3 | 1    |    1 | row1 |
|    4 | row4 | 1    |    1 | row1 |
|    5 | row5 | NULL | NULL | NULL |
+------+------+------+------+------+

现在我们在_ rows of get table匹配B,but only _ rows of table。P></

right join table about the second only的关心P></

a RIGHT JOIN b得到确切的说因为是the same as b LEFT JOIN arows。差分阶the only is the columns of the默认。P></

1
2
3
4
5
6
7
8
> SELECT * FROM table_a a RIGHT JOIN table_b b ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | id   | name | aid  |
+------+------+------+------+------+
|    1 | row1 |    3 | row3 | 1    |
|    1 | row1 |    4 | row4 | 1    |
| NULL | NULL |    5 | row5 | NULL |
+------+------+------+------+------+

This is the same as table_b LEFT JOIN table_arows,which we the left join在截面看到。P></

similarly:P></

1
2
3
4
5
6
7
8
> SELECT * FROM table_b b RIGHT JOIN table_a a ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | aid  | id   | name |
+------+------+------+------+------+
|    3 | row3 | 1    |    1 | row1 |
|    4 | row4 | 1    |    1 | row1 |
| NULL | NULL | NULL |    2 | row2 |
+------+------+------+------+------+

is the same as table_a LEFT JOIN table_brows。P></

不给你拷贝用at all of MindP></

如果你不写你的表与连接在所有的条款,只是commas separated by,You get table of the first所写的每行每行of the second下表,在每个可能的组合:P></

1
2
3
4
5
6
7
8
9
10
11
> SELECT * FROM table_b b, table_a;
+------+------+------+------+------+
| id   | name | aid  | id   | name |
+------+------+------+------+------+
|    3 | row3 | 1    |    1 | row1 |
|    3 | row3 | 1    |    2 | row2 |
|    4 | row4 | 1    |    1 | row1 |
|    4 | row4 | 1    |    2 | row2 |
|    5 | row5 | NULL |    1 | row1 |
|    5 | row5 | NULL |    2 | row2 |
+------+------+------+------+------+

(this is from examples of SQL连接我的博客类型)P></