gpt4 book ai didi

sql - 加入一个普通表,你如何得到一个 FULL OUTER JOIN 来扩展另一个表?

转载 作者:行者123 更新时间:2023-12-04 21:49:40 24 4
gpt4 key购买 nike

我已在 StackOverflow 和 Google 上搜索过这个问题的答案。

我正在尝试创建一个 Microsot SQL Server 2008 View 。不是存储过程。不是函数。只是一个查询(即一个 View )。

我有三个表。第一个表定义了一个公共(public)键,比方说“CompanyID”。其他两个表有时有一个公共(public)字段,假设为“EmployeeName”。

我想要一个表结果,当我的 WHERE 子句说“WHERE CompanyID = 12”时,它看起来像这样:

CompanyID | TableA    | TableB
12 | John Doe | John Doe
12 | Betty Sue | NULL
12 | NULL | Billy Bob

我试过一个 FULL OUTER JOIN 看起来像这样:

SELECT Company.CompanyID,
TableA.EmployeeName,
TableB.EmployeeName
FROM Company
FULL OUTER JOIN TableA ON Company.CompanyID = TableA.CompanyID
FULL OUTER JOIN TableB ON
Company.CompanyID = TableB.CompanyID AND
(TableA.EmployeeName IS NULL OR TableB.EmployeeName IS NULL OR TableB.EmployeeName = TableA.EmployeeName)

我只从一个匹配的表中得到 NULL,我没有得到另一个表的扩展。在上面的示例中,我基本上只得到第一行和第三行,而不是第二行。

有人可以帮我创建这个查询并告诉我如何正确完成吗?

顺便说一句,我已经有一个看起来非常干净并填充内存表的存储过程,但这不是我想要的。

谢谢。

-- 编辑:

这是当前不工作的完整运行示例(它缺少“某人 2”和“某人 3”。

DECLARE @Company TABLE
(
CompanyID int
)

INSERT INTO @Company (CompanyID) VALUES (10)
INSERT INTO @Company (CompanyID) VALUES (12)

DECLARE @TableA TABLE
(
EmployeeId int,
CompanyId int,
EmployeeName varchar(30)
)

DECLARE @TableB TABLE
(
EmployeeId int,
CompanyId int,
EmployeeName varchar(30)
)

INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 1, 10, 'someone' )

--INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )
--VALUES ( 2, 12, 'someone 2' )

INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 3, 12, 'someone 3' )

INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 3, 12, 'someone 4' )

INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 1, 10, 'someone' )

INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 2, 12, 'someone 2' )

--INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )
--VALUES ( 3, 12, 'someone 3' )

INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )
VALUES ( 3, 12, 'someone 4' )

SELECT Company.CompanyID,
TableA.EmployeeName,
TableB.EmployeeName
FROM @Company Company
FULL OUTER JOIN @TableA TableA ON Company.CompanyID = TableA.CompanyID
FULL OUTER JOIN @TableB TableB ON Company.CompanyID = TableB.CompanyID
WHERE
(
TableA.EmployeeName IS NULL OR TableB.EmployeeName IS NULL OR
TableB.EmployeeName = TableA.EmployeeName
)
AND Company.CompanyID = 12

结果:

CompanyID   EmployeeName    EmployeeName
12 someone 4 someone 4

我想要的:

CompanyID   EmployeeName    EmployeeName
12 NULL someone 2
12 someone 3 NULL
12 someone 4 someone 4

最佳答案

试试这个:

SELECT Company.CompanyID,
TableA.EmployeeName,
TableB.EmployeeName
FROM Company
LEFT OUTER JOIN TableA ON Company.CompanyID = TableA.CompanyID
LEFT OUTER JOIN TableB ON Company.CompanyID = TableB.CompanyID
WHERE (TableA.EmployeeName IS NULL OR TableB.EmployeeName IS NULL OR TableB.EmployeeName = TableA.EmployeeName)

EDIT OP给出测试数据和预期结果集

试试这个(来自问题的表格和测试数据):

DECLARE @Company TABLE (CompanyID int)
DECLARE @TableA TABLE (EmployeeId int,CompanyId int,EmployeeName varchar(30))
DECLARE @TableB TABLE (EmployeeId int,CompanyId int,EmployeeName varchar(30))

set nocount on
INSERT INTO @Company (CompanyID) VALUES (10)
INSERT INTO @Company (CompanyID) VALUES (12)

--INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )--VALUES ( 2, 12, 'someone 2' )
INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )VALUES ( 1, 10, 'someone' )
INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )VALUES ( 3, 12, 'someone 3' )
INSERT INTO @TableA ( EmployeeId, CompanyId, EmployeeName )VALUES ( 3, 12, 'someone 4' )

--INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )--VALUES ( 3, 12, 'someone 3' )
INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )VALUES ( 1, 10, 'someone' )
INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )VALUES ( 2, 12, 'someone 2' )
INSERT INTO @TableB ( EmployeeId, CompanyId, EmployeeName )VALUES ( 3, 12, 'someone 4' )
set nocount off

SELECT coalesce(TableA.CompanyID,TableB.CompanyID) CompanyID,
TableA.EmployeeName,
TableB.EmployeeName
FROM @TableA TableA
FULL OUTER jOIN @TableB TableB ON TableA.CompanyID = TableB.CompanyID AND TableB.EmployeeName = TableA.EmployeeName
WHERE coalesce(TableA.CompanyID,TableB.CompanyID) = 12

输出:

CompanyID   EmployeeName                   EmployeeName
----------- ------------------------------ ------------------------------
12 NULL someone 2
12 someone 3 NULL
12 someone 4 someone 4

(3 row(s) affected)

关于sql - 加入一个普通表,你如何得到一个 FULL OUTER JOIN 来扩展另一个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2518042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com