Sql server 如何返回其他表中不存在的行
我有一张这样的桌子:Sql server 如何返回其他表中不存在的行,sql-server,tsql,Sql Server,Tsql,我有一张这样的桌子: CompanyID | SectionID | Service Name ------------------------------------ 1 | 1 | AAAAAAA 1 | 2 | BBBBBBB 2 | 1 | CCCCCCC 表b如下所示: InspectionID | CompanyID | SectionID ------------
CompanyID | SectionID | Service Name
------------------------------------
1 | 1 | AAAAAAA
1 | 2 | BBBBBBB
2 | 1 | CCCCCCC
表b如下所示:
InspectionID | CompanyID | SectionID
-------------------------------------
1 | 1 | 2
2 | 2 | 1
SELECT distinct [Service name]
FROM tableA a
left join tableb b on a.companyId = b.companyId
and a.sectionId = b.sectionId
where b.companyId is null
我需要一个SQL命令,该命令返回没有相关检查的每个服务名称(来自表a)(表B由CompanyID
和SectionID
连接)
就像这样:
Service Name
------------
BBBBBBB
谢谢 您可以这样做:
InspectionID | CompanyID | SectionID
-------------------------------------
1 | 1 | 2
2 | 2 | 1
SELECT distinct [Service name]
FROM tableA a
left join tableb b on a.companyId = b.companyId
and a.sectionId = b.sectionId
where b.companyId is null
或
如果是这种情况,您的输出应该是AAAAA。我说得对吗?如果我错了,请告诉我怎么做。在互联网上寻找“不存在的地方”的例子。因为SectionID@KritnerI编辑了这个问题以澄清问题