制定 TSQL 左连接

发布于 2024-12-09 03:10:16 字数 429 浏览 1 评论 0原文

我正在使用 SSMS 2008。应该是一个简单的解决方案。我试图从表 A 中提取所有记录,但只从表 B 和 C 中提取匹配的记录。问题是,如果我保留连接表 B 和 C,它会返回所有 3 个表相交的记录。就像如果我查询这个组合合并,我会得到 B 和 C 的每一行的非 NULL 值。

这是我的伪代码:

SELECT A.ID, B.ID, C.ID
FROM A
LEFT JOIN B ON B.ID = A.ID
LEFT JOIN C ON C.ID = A.ID

在回答你的问题时,很抱歉我忘记了“LEFT”,但我只是在上面添加了它。如果表A有9行,B有2行,C有3行,那么我想在上面看到的是表A与B相交以及A与C相交的位置。 所以在刚才描述的场景中,假设表 B 的行与表 C 的行全部不同,那么我想看到总共 5 行; B 为 2,C 为 3。有意义吗?

I am using SSMS 2008. Should be a simple solution to this. I am trying to pull all records from table A, but only pull in matching records from tables B and C. The problem is that if I left join tables B and C, it returns records where all 3 of these tables intersect instead. Like if I query this combined merge, I get non-NULL values for every row for both B and C.

Here is my pseudocode:

SELECT A.ID, B.ID, C.ID
FROM A
LEFT JOIN B ON B.ID = A.ID
LEFT JOIN C ON C.ID = A.ID

In answer to your questions, I am sorry I forgot the "LEFT", but I just added it above. If table A has 9 rows and B has 2 rows and C has 3 rows, then what I want to see above is where table A intersects B and where A intersects C.
So in the scenario just described, assuming that the Table B rows are all different than the Table C rows, then I want to see a total of 5 rows; 2 from B and 3 from C. Make sense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瞄了个咪的 2024-12-16 03:10:17

问题陈述仍然不一致。我认为你的意思是表 A 与 B 相交或 A 与 C 相交。在这种情况下,正确的 TSQL 是:

    SELECT A.ID
    FROM A
    LEFT OUTER JOIN B ON B.ID = A.ID
    LEFT OUTER JOIN C ON C.ID = A.ID
    WHERE B.ID IS NOT NULL OR C.ID IS NOT NULL 
    GROUP BY A.ID 
    ORDER BY A.ID

如果 B.ID 和 C.ID 等于 A.ID,则无需报告它们。
Group By 是为了避免重复 A.ID。

The problem statement is still inconsistent. I think you mean where table A intersects B OR where A intersects C. In that case the proper TSQL is:

    SELECT A.ID
    FROM A
    LEFT OUTER JOIN B ON B.ID = A.ID
    LEFT OUTER JOIN C ON C.ID = A.ID
    WHERE B.ID IS NOT NULL OR C.ID IS NOT NULL 
    GROUP BY A.ID 
    ORDER BY A.ID

No need to report B.ID and C.ID if they equal A.ID.
Group By is so you don't get repeated A.ID.

Smile简单爱 2024-12-16 03:10:17
SELECT A.ID, B.ID, C.ID
FROM A
LEFT JOIN B ON B.ID = A.ID
LEFT JOIN C ON C.ID = A.ID
WHERE B.ID IS NOT NULL OR C.ID IS NOT NULL

或者,如果您只需要 A 中的字段,那么

SELECT A.ID
FROM A
INNER JOIN (
    SELECT B.ID FROM B
    UNION
    SELECT C.ID FROM C ) AS U ON U.ID = A.ID
SELECT A.ID, B.ID, C.ID
FROM A
LEFT JOIN B ON B.ID = A.ID
LEFT JOIN C ON C.ID = A.ID
WHERE B.ID IS NOT NULL OR C.ID IS NOT NULL

Or, if you need only fields from A, then

SELECT A.ID
FROM A
INNER JOIN (
    SELECT B.ID FROM B
    UNION
    SELECT C.ID FROM C ) AS U ON U.ID = A.ID
阪姬 2024-12-16 03:10:16

实现此目的的理想方法是使用LEFT JOIN。我相信(尽管从您的问题中不清楚)您遇到的问题是每个 A.ID 有多行。

如果 A.IDB.ID AND C.ID 匹配,那么您将获得两行,并且想要一个合并行。

我认为您的测试查询中可能混合了 JOIN 条件。这对我来说在测试中工作得很好。尝试以下查询:

DECLARE @A TABLE (ID INT)
DECLARE @B TABLE (ID INT)
DECLARE @C TABLE (ID INT)

INSERT INTO @A VALUES
(1),
(2),
(3),
(4)

INSERT INTO @B VALUES
(2),
(3)

INSERT INTO @C VALUES
(3),
(4)

SELECT A.ID, B.ID, C.ID
FROM @A A
LEFT JOIN @B B
    ON B.ID = A.ID
LEFT JOIN @C C
    ON C.ID = A.ID

输出为:

ID  ID      ID
1   NULL    NULL
2   2       NULL
3   3       3
4   NULL    4

The ideal way to do this is with a LEFT JOIN. I believe (though it's unclear from your question) that the problem you have is multiple rows per A.ID.

If A.ID matches B.ID AND C.ID then you are getting two rows for this, and would like one consolidated row.

I think maybe you had your JOIN conditions mixed up in your test query. This is working fine for me in testing. Try the below query:

DECLARE @A TABLE (ID INT)
DECLARE @B TABLE (ID INT)
DECLARE @C TABLE (ID INT)

INSERT INTO @A VALUES
(1),
(2),
(3),
(4)

INSERT INTO @B VALUES
(2),
(3)

INSERT INTO @C VALUES
(3),
(4)

SELECT A.ID, B.ID, C.ID
FROM @A A
LEFT JOIN @B B
    ON B.ID = A.ID
LEFT JOIN @C C
    ON C.ID = A.ID

Output is:

ID  ID      ID
1   NULL    NULL
2   2       NULL
3   3       3
4   NULL    4
画尸师 2024-12-16 03:10:16

你想要的是加入工会声明。

SELECT A.ID, 
case when D.tablename = 'B' then  ID else null end, 
case when D.tablename = 'C' then  ID else null end
FROM A 
JOIN 
(select Id, 'b' as tablename from B
union all
select id, 'c' from c)
D ON D.ID = A.ID 

what you want is a join to a union stamtement.

SELECT A.ID, 
case when D.tablename = 'B' then  ID else null end, 
case when D.tablename = 'C' then  ID else null end
FROM A 
JOIN 
(select Id, 'b' as tablename from B
union all
select id, 'c' from c)
D ON D.ID = A.ID 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文