SQL JOIN 和 INNER JOIN 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/565620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:11:09  来源:igfitidea点击:

Difference between JOIN and INNER JOIN

sqlsql-serverjoininner-join

提问by driis

Both these joins will give me the same results:

这两个连接都会给我相同的结果:

SELECT * FROM table JOIN otherTable ON table.ID = otherTable.FK

vs

对比

SELECT * FROM table INNER JOIN otherTable ON table.ID = otherTable.FK

Is there any difference between the statements in performance or otherwise?

性能或其他方面的陈述之间有什么区别吗?

Does it differ between different SQLimplementations?

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

回答by palehorse

They are functionally equivalent, but INNER JOINcan be a bit clearer to read, especially if the query has other join types (i.e. LEFTor RIGHTor CROSS) included in it.

它们在功能上是等效的,但INNER JOIN读起来会更清晰一些,尤其是当查询中包含其他连接类型(即LEFTorRIGHTCROSS)时。

回答by Quassnoi

No, there is no difference, pure syntactic sugar.

不,没有区别,纯语法糖

回答by net_prog

INNER JOIN = JOIN

内连接 = 连接

INNER JOIN is the default if you don't specifythe 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.

如果您在使用 JOIN 一词时未指定类型,INNER JOIN 是默认值

您还可以使用 LEFT OUTER JOIN 或 RIGHT OUTER JOIN,在这种情况下,OUTER 一词是可选的,或者您可以指定 CROSS JOIN。

OR

或者

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)

对于内部联接,语法为:

SELECT ...
FROM TableA
[INNER] JOIN TableB

(换句话说,“INNER”关键字是可选的- 无论有没有它,结果都是一样的)

回答by Micha? Powaga

Does it differ between different SQL implementations?

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

Yes, Microsoft Accessdoesn't allow just join. It requires inner join.

是的,Microsoft Access不允许仅join. 它需要inner join.

回答by Kristen

Similarly with OUTER JOINs, the word "OUTER"is optional. It's the LEFTor RIGHTkeyword that makes the JOINan "OUTER" JOIN.

与 类似OUTER JOINs,这个词"OUTER"是可选的。是LEFTorRIGHT关键字使JOINan "OUTER" JOIN

However for some reason I always use "OUTER"as in LEFT OUTER JOINand never LEFT JOIN, but I never use INNER JOIN, but rather I just use "JOIN":

但是出于某种原因,我总是使用"OUTER"as inLEFT OUTER JOIN并且从不使用LEFT JOIN,但我从不使用INNER JOIN,而是我只是使用"JOIN"

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

回答by Martin Smith

As the other answers already state there is no difference in your example.

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

The relevant bit of grammar is documented here

相关的语法部分记录在此处

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

Showing that all are optional. The page further clarifies that

显示所有都是可选的。该页面进一步阐明

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

INNER指定返回所有匹配的行对。丢弃两个表中不匹配的行。当未指定联接类型时,这是默认值

The grammar does also indicate that there is one time where the INNERisrequired though. When specifying a join hint.

语法确实也表明,有其中一次INNER必需的,但。指定连接提示时。

See the example below

看下面的例子

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

在此处输入图片说明