此外,如何适应左外加入,右外加入和全外加入?


当前回答

一次示威

设置

跳进 psql 并创建一个小数据库的猫和人类. 你可以只是复制这个整个部分。

CREATE DATABASE catdb;
\c catdb;
\pset null '[NULL]' -- how to display null values

CREATE TABLE humans (
  name text primary key
);
CREATE TABLE cats (
  human_name text references humans(name),
  name text
);

INSERT INTO humans (name)
VALUES ('Abe'), ('Ann'), ('Ben'), ('Jen');

INSERT INTO cats (human_name, name)
VALUES
('Abe', 'Axel'),
(NULL, 'Bitty'),
('Jen', 'Jellybean'),
('Jen', 'Juniper');

求求

SELECT
humans.name AS human_name,
cats.name AS cat_name
FROM humans
[SOMETHING JOIN] cats ON humans.name = cats.human_name
ORDER BY humans.name;

任何人,没有猫或猫,没有人,都被排除在外。

 human_name | cat_name
------------+-----------
 Abe        | Axel
 Jen        | Jellybean
 Jen        | Juniper

A FULL OUTER JOIN returns all humans and all cats, with zero if there is no match on both sides. 全外加入将返回所有人类和所有猫,如果双方都没有比赛,则零。

 human_name | cat_name
------------+-----------
 Abe        | Axel
 Ann        | [NULL]
 Ben        | [NULL]
 Jen        | Jellybean
 Jen        | Juniper
 [NULL]     | Bitty

 human_name | cat_name
------------+-----------
 Abe        | Axel
 Ann        | [NULL]
 Ben        | [NULL]
 Jen        | Jellybean
 Jen        | Juniper

每只猫,没有人,都会在人名列中获得零;每只猫,没有人,都会被排除。

 human_name | cat_name
------------+-----------
 Abe        | Axel
 Jen        | Jellybean
 Jen        | Juniper
 [NULL]     | Bitty

你可以看到,虽然一个内部加入只获得匹配夫妇,每个类型的外部加入包括一些没有比赛的项目。

加入本身意味着内部左加入,右加入和外加入,所有意味着外加入。

其他回答

假设你加入没有复制的列,这是一个非常常见的案例:

A 和 B 的内部结合会产生 A 交叉B 的结果,即 Venn 图表交叉的内部部分; A 和 B 的外部结合会产生 A 联盟 B 的结果,即 Venn 图表联盟的外部部分。

例子

假设你有两个表,每个单列,并数据如下:

A    B
-    -
1    3
2    4
3    5
4    6

内部加入

使用相同的查询中的任何一个内部连接提供两张表的交叉,即它们共通的两行。

select * from a INNER JOIN b on a.a = b.b;
select a.*, b.*  from a,b where a.a = b.b;

a | b
--+--
3 | 3
4 | 4

左边加入

左向外加入将提供A中的所有行,加上B中的任何常见行。

select * from a LEFT OUTER JOIN b on a.a = b.b;
select a.*, b.*  from a,b where a.a = b.b(+);

a |  b
--+-----
1 | null
2 | null
3 |    3
4 |    4

向外加入

一个正确的外部加入将给所有行在B,加上任何常见行在A。

select * from a RIGHT OUTER JOIN b on a.a = b.b;
select a.*, b.*  from a,b where a.a(+) = b.b;

a    |  b
-----+----
3    |  3
4    |  4
null |  5
null |  6

全外加入

一个完整的外部加入会给你A和B的联盟,即A中的所有行和B中的所有行,如果A中的任何东西没有相应的日期在B,那么B部分是零,反之亦然。

select * from a FULL OUTER JOIN b on a.a = b.b;

 a   |  b
-----+-----
   1 | null
   2 | null
   3 |    3
   4 |    4
null |    6
null |    5

内部加入

回收相匹配的行只,也就是说, A 交叉 B。

此分類上一篇

SELECT *
FROM dbo.Students S
INNER JOIN dbo.Advisors A
    ON S.Advisor_ID = A.Advisor_ID

左向外加入

从第一张表中选择所有记录,以及与附件密钥相匹配的第二张表中的任何记录。

此分類上一篇

SELECT *
FROM dbo.Students S
LEFT JOIN dbo.Advisors A
    ON S.Advisor_ID = A.Advisor_ID

全外加入

从第二张表中选择所有记录,以及与附件密钥相匹配的第一张表中的任何记录。

此分類上一篇

SELECT *
FROM dbo.Students S
FULL JOIN dbo.Advisors A
    ON S.Advisor_ID = A.Advisor_ID

参考

内部和外部加入 SQL 示例和加入区块 SQL: JOINS

EmpID EmpName
13 Jason
8 Alex
3 Ram
17 Babu
25 Johnson

位置

EmpID EmpLoc
13 San Jose
8 Los Angeles
3 Pune, India
17 Chennai, India
39 Bangalore, India

select  * from employee inner join location on employee.empID = location.empID
OR
select  * from employee, location where employee.empID = location.empID

Employee.EmpId Employee.EmpName Location.EmpId Location.EmpLoc
13 Jason 13 San Jose
8 Alex 8 Los Angeles
3 Ram 3 Pune, India
17 Babu 17 Chennai, India

左外加入:- 对员工和位置表的左外加入(或仅仅是左外加入)的结果总是包含“左”表(员工)的所有记录,即使加入条件在“右”表(位置)中找不到任何匹配记录。

select  * from employee left outer join location on employee.empID = location.empID;
//Use of outer keyword is optional

Employee.EmpId Employee.EmpName Location.EmpId Location.EmpLoc
13 Jason 13 San Jose
8 Alex 8 Los Angeles
3 Ram 3 Pune, India
17 Babu 17 Chennai, India
25 Johnson NULL NULL

右外登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登登

select * from employee right outer join location  on employee.empID = location.empID;
//Use of outer keyword is optional

使用上面的表,我们可以显示一个正确的外部加入的结果设置会是什么样子:

Employee.EmpId Employee.EmpName Location.EmpId Location.EmpLoc
13 Jason 13 San Jose
8 Alex 8 Los Angeles
3 Ram 3 Pune, India
17 Babu 17 Chennai, India
NULL NULL 39 Bangalore, India

请注意,虽然没有员工被列为在巴格罗尔工作,但它仍然被列入结果中,员工字段被删除。

Employee.EmpId Employee.EmpName Location.EmpId Location.EmpLoc
13 Jason 13 San Jose
8 Alex 8 Los Angeles
3 Ram 3 Pune, India
17 Babu 17 Chennai, India
25 Johnson NULL NULL
NULL NULL 39 Bangalore, India

Oracle 加入活动

内部加入需要附加表中包含相关 ID 的记录。

外部连接将为左侧返回记录,即使没有对右侧的记录。

例如,您有一个订单和一个订单细节表,它们由一个“订单ID”相关。

命令

客戶名稱

命令细节

商品名稱 Qty 價格

请求

SELECT Orders.OrderID, Orders.CustomerName
  FROM Orders 
 INNER JOIN OrderDetails
    ON Orders.OrderID = OrderDetails.OrderID

只会返回订单,该订单还包含在订单详细信息表中的一些东西。

如果您将其更改为向左加入

SELECT Orders.OrderID, Orders.CustomerName
  FROM Orders 
  LEFT JOIN OrderDetails
    ON Orders.OrderID = OrderDetails.OrderID

然后它将从订单表中返回记录,即使它们没有订单详细记录。

您可以使用此来查找没有订单详细信息的订单,并通过添加一个在哪里条款,如Where OrderDetails.OrderID IS NULL。

您使用 INNER JOIN 返回两个表中的所有行,即在结果表中,所有行和列都将有值。

在 OUTER JOIN 中,结果表可能有空列,外部加入可能是左或右。

LEFT OUTER JOIN 从第一张桌子中返回所有行,即使在第二张桌子中没有比赛。

RIGHT OUTER JOIN 从第二张桌子返回所有行,即使在第一张桌子上没有比赛。