INNER JOIN,LEFT JOIN,RIGHT JOIN和FULL JOIN有什么区别? [重复]

本文翻译自:What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

What's the difference between INNER JOIN , LEFT JOIN , RIGHT JOIN and FULL JOIN in MySQL ? MySQL中的 INNER JOINLEFT JOINRIGHT JOINFULL JOIN什么区别?


#1楼

参考:https://stackoom.com/question/NwVJ/INNER-JOIN-LEFT-JOIN-RIGHT-JOIN和FULL-JOIN有什么区别-重复


#2楼

An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them. SQL JOIN子句用于根据两个或多个表之间的公共字段合并行。

There are different types of joins available in SQL: SQL中有不同类型的联接:

INNER JOIN : returns rows when there is a match in both tables. INNER JOIN :两个表中都匹配时返回行。

LEFT JOIN : returns all rows from the left table, even if there are no matches in the right table. LEFT JOIN :即使左侧表中没有匹配项,也返回左侧表中的所有行。

RIGHT JOIN : returns all rows from the right table, even if there are no matches in the left table. RIGHT JOIN :即使左侧表中没有匹配项,也返回右侧表中的所有行。

FULL JOIN : It combines the results of both left and right outer joins. FULL JOIN :它合并了左右外部联接的结果。

The joined table will contain all records from both the tables and fill in NULLs for missing matches on either side. 联接的表将包含两个表中的所有记录,并为任一侧缺少的匹配项填充NULL。

SELF JOIN : is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement. SELF JOIN :用于将一个表连接到自身,就像该表是两个表一样,在SQL语句中临时重命名至少一个表。

CARTESIAN JOIN : returns the Cartesian product of the sets of records from the two or more joined tables. CARTESIAN JOIN :从两个或多个联接表中返回记录集的笛卡尔积。

WE can take each first four joins in Details : 我们可以在“详细信息”中进行前四个联接:

We have two tables with the following values. 我们有两个具有以下值的表。

TableA 表A

id  firstName                  lastName
.......................................
1   arun                        prasanth                 
2   ann                         antony                   
3   sruthy                      abc                      
6   new                         abc                                           

TableB 表B

id2 age Place
................
1   24  kerala
2   24  usa
3   25  ekm
5   24  chennai

.................................................................... ................................................... .....................

INNER JOIN 内部联接

Note :it gives the intersection of the two tables, ie rows they have common in TableA and TableB 注意 :它给出了两个表的交集,即它们在TableA和TableB中共有的行

Syntax 句法

SELECT table1.column1, table2.column2...
  FROM table1
 INNER JOIN table2
    ON table1.common_field = table2.common_field;

Apply it in our sample table : 将其应用到我们的示例表中:

SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place
  FROM TableA
 INNER JOIN TableB
    ON TableA.id = TableB.id2;

Result Will Be 结果将是

firstName       lastName       age  Place
..............................................
arun            prasanth        24  kerala
ann             antony          24  usa
sruthy          abc             25  ekm

LEFT JOIN 左联接

Note : will give all selected rows in TableA, plus any common selected rows in TableB. 注意 :将提供TableA中所有选定的行,以及TableB中所有常见的选定行。

Syntax 句法

SELECT table1.column1, table2.column2...
  FROM table1
  LEFT JOIN table2
    ON table1.common_field = table2.common_field;

Apply it in our sample table : 将其应用到我们的示例表中:

SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place
  FROM TableA
  LEFT JOIN TableB
    ON TableA.id = TableB.id2;

Result 结果

firstName                   lastName                    age   Place
...............................................................................
arun                        prasanth                    24    kerala
ann                         antony                      24    usa
sruthy                      abc                         25    ekm
new                         abc                         NULL  NULL

RIGHT JOIN 正确加入

Note : will give all selected rows in TableB, plus any common selected rows in TableA. 注意 :将提供TableB中的所有选定行,以及TableA中的所有常见选定行。

Syntax 句法

SELECT table1.column1, table2.column2...
  FROM table1
 RIGHT JOIN table2
    ON table1.common_field = table2.common_field;

Apply it in our sample table : 将其应用到我们的示例表中:

SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place
  FROM TableA
 RIGHT JOIN TableB
    ON TableA.id = TableB.id2;

Result 结果

firstName                   lastName                    age     Place
...............................................................................
arun                        prasanth                    24     kerala
ann                         antony                      24     usa
sruthy                      abc                         25     ekm
NULL                        NULL                        24     chennai

FULL JOIN 完全加入

Note :It will return all selected values from both tables. 注意 :它将返回两个表中的所有选定值。

Syntax 句法

SELECT table1.column1, table2.column2...
  FROM table1
  FULL JOIN table2
    ON table1.common_field = table2.common_field;

Apply it in our sample table : 将其应用到我们的示例表中:

SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place
  FROM TableA
  FULL JOIN TableB
    ON TableA.id = TableB.id2;

Result 结果

firstName                   lastName                    age    Place
...............................................................................
arun                        prasanth                    24    kerala
ann                         antony                      24    usa
sruthy                      abc                         25    ekm
new                         abc                         NULL  NULL
NULL                        NULL                        24    chennai

Interesting Fact 有趣的事实

For INNER joins the order doesn't matter 对于INNER加入,顺序无关紧要

For (LEFT, RIGHT or FULL) OUTER joins,the order matter 对于(LEFT,RIGHT或FULL)OUTER加入,顺序很重要

Better to go check this Link it will give you interesting details about join order 最好去检查此链接 ,它将为您提供有关加入顺序的有趣详细信息


#3楼

INNER JOIN gets all records that are common between both tables based on the supplied ON clause. INNER JOIN根据提供的ON子句获取两个表之间共有的所有记录。

LEFT JOIN gets all records from the LEFT linked table but if you have selected some columns from the RIGHT table, if there is no related records, these columns will contain NULL. LEFT JOIN从LEFT链接表中获取所有记录,但是如果您从RIGHT表中选择了某些列,则如果没有相关记录,则这些列将包含NULL。

RIGHT JOIN is like the above but gets all records in the RIGHT table. RIGHT JOIN与上面类似,但是获取RIGHT表中的所有记录。

FULL JOIN gets all records from both tables and puts NULL in the columns where related records do not exist in the opposite table. FULL JOIN从两个表中获取所有记录,并将NULL放入相反表中不存在相关记录的列中。


#4楼

Reading this original article on The Code Project will help you a lot: Visual Representation of SQL Joins . 阅读有关代码项目的原始文章将对您有很大帮助: SQL Joins的可视表示

替代文字

Also check this post: SQL SERVER – Better Performance – LEFT JOIN or NOT IN? 还请检查此帖子: SQL SERVER –更好的性能–左联接还是不联接? .

Find original one at: Difference between JOIN and OUTER JOIN in MySQL . 在以下位置找到原始版本: MySQL中的JOIN和OUTER JOIN之间的区别

发布了0 篇原创文章 · 获赞 72 · 访问量 54万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105202038