关于SQL:联接和内部联接的区别

Difference between JOIN and INNER JOIN

这两个联接都将给出相同的结果:

1
SELECT * FROM TABLE JOIN otherTable ON TABLE.ID = otherTable.FK

VS

1
SELECT * FROM TABLE INNER JOIN otherTable ON TABLE.ID = otherTable.FK

在履行或其他方面的陈述有什么不同吗?

不同的SQL实现之间有区别吗?


它们在功能上是等效的,但是INNER JOIN可以更清晰地阅读,特别是当查询中包含其他连接类型(即LEFTRIGHTCROSS)时。


不,没有区别,纯句法糖。


内部联接=联接:

INNER JOIN is the default if you don't specify the type when you use the word JOIN.

You can also use LEFT OUTER JOIN or RIGHT OUTER JOIN, in which case the word OUTER is optional,
or you can specify CROSS JOIN.

For an inner join, the syntax is:

SELECT ...
FROM TableA
[INNER] JOIN TableB

(in other words, the"INNER" keyword is optional - results are the same
with or without it)


OUTER JOINs类似,单词"OUTER"是可选的。是LEFTRIGHT关键字使JOIN成为"OUTER" JOIN

然而,由于某种原因,我总是使用"OUTER",就像在LEFT OUTER JOIN中一样,从不使用LEFT JOIN,但我从不使用INNER JOIN,而是只使用"JOIN"

1
2
3
4
5
6
SELECT ColA, ColB, ...
FROM MyTable AS T1
     JOIN MyOtherTable AS T2
         ON T2.ID = T1.ID
     LEFT OUTER JOIN MyOptionalTable AS T3
         ON T3.ID = T1.ID


Does it differ between different SQL implementations?

是的,Microsoft Access不允许仅使用JOIN。它需要INNER JOIN


正如其他答案已经指出的那样,在您的示例中没有区别。

文法的相关部分记录在这里

1
2
3
<join_type> ::=
    [ { INNER | { { LEFT | RIGHT | FULL } [ OUTER ] } } [ <join_hint> ] ]
    JOIN

显示所有选项都是可选的。这一页进一步澄清了

INNER Specifies all matching pairs of rows are returned. Discards
unmatched rows from both tables. When no join type is specified, this
is the default.

语法也表明有一次需要使用INNER。指定联接提示时。

见下面的例子

1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE T1(X INT);
CREATE TABLE T2(Y INT);

SELECT *
FROM   T1
       LOOP JOIN T2
         ON X = Y;

SELECT *
FROM   T1
       INNER LOOP JOIN T2
         ON X = Y;

enter image description here