【问题标题】:Retrieve non matching record using join使用连接检索不匹配的记录
【发布时间】:2011-09-29 21:57:56
【问题描述】:

我有两张桌子,桌子 A 和桌子 B。

两者都有 4 条匹配记录,表 A 包含 6 条不匹配的记录。

在join的帮助下,我如何检索不匹配的记录?

【问题讨论】:

  • 您想返回 10 行(4 个常见的 + 6 个来自 A)还是只返回 6 个来自 A 的不匹配?

标签: sql-server


【解决方案1】:

您可以使用left outer join 并测试B.ID is null。此示例将在 SQL Server 2008 中运行,但查询在此之前的版本中有效。

declare @TableA table (ID int)
declare @TableB table (ID int)

insert into @TableA values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)
insert into @TableB values (1),(2),(3),(4)

select A.*
from @TableA as A
  left outer join @TableB as B
    on A.ID = B.ID
where B.ID is null

结果:

ID
--
5
6
7
8
9
10

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    相关资源
    最近更新 更多