简体   繁体   English

SQL 服务器:select 记录,未链接到另一个表

[英]SQL Server: select records, not linked to another table

I have a table:我有一张桌子:

CREATE TABLE [dbo].[CollectionSite]
(
    [SiteCode] [nvarchar](32) NOT NULL,
    [AddressId] [int] NOT NULL,
    [RemittanceId] [int] NULL,
    // additional columns
)

and a linked table:和一个链接表:

CREATE TABLE [dbo].[CollectionSiteAddress]
(
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](255) NULL,
    [Address1] [nvarchar](255) NULL,
    [Address2] [nvarchar](255) NULL,
    [City] [nvarchar](128) NULL,
    [State] [nvarchar](64) NULL,
    [Zip] [nvarchar](32) NULL,
)

Relationship between these 2 tables:这两个表之间的关系:

ALTER TABLE [dbo].[CollectionSite] WITH CHECK 
    ADD CONSTRAINT [FK_CollectionSite_CollectionSiteAddress_AddressId] 
        FOREIGN KEY([AddressId]) REFERENCES [dbo].[CollectionSiteAddress] ([Id])
GO

ALTER TABLE [dbo].[CollectionSite]  WITH CHECK 
    ADD CONSTRAINT [FK_CollectionSite_CollectionSiteAddress_RemittanceId]  
        FOREIGN KEY([RemittanceId]) REFERENCES [dbo].[CollectionSiteAddress] ([Id])
GO

I want to select all records from CollectionSiteAddress , which are not linked to CollectionSite (neither AddressId nor RemittanceId ).我想 select 来自CollectionSiteAddress的所有记录,这些记录未链接到CollectionSite (既不是AddressId也不是RemittanceId )。 Which request should I use?我应该使用哪个请求?

I tried:我试过了:

SELECT *
FROM CollectionSiteAddress 
LEFT JOIN CollectionSite ON CollectionSiteAddress.Id = CollectionSite.AddressId 
                         OR CollectionSiteAddress.Id = CollectionSite.RemittanceId

but it selects all records from CollectionSiteAddress但它从CollectionSiteAddress中选择所有记录

You are missing this WHERE clause:您缺少此WHERE子句:

WHERE CollectionSite.[SiteCode] IS NULL

because you want all the unmatched rows of CollectionSiteAddress .因为您想要CollectionSiteAddress的所有不匹配行。
I used the column [SiteCode] to check if it is NULL because it is not nullable in the definition of the table.我使用[SiteCode]列检查它是否为NULL ,因为它在表的定义中不可为空。
So you can write your query like this (shortened with aliases):所以你可以这样写你的查询(用别名缩短):

SELECT csa.*
FROM CollectionSiteAddress csa LEFT JOIN CollectionSite cs
ON csa.Id = cs.AddressId OR csa.Id = cs.RemittanceId
WHERE cs.[SiteCode] IS NULL

Or use NOT EXISTS :或使用NOT EXISTS

SELECT csa.*
FROM CollectionSiteAddress csa 
WHERE NOT EXISTS (
  SELECT 1 
  FROM CollectionSite cs
  WHERE csa.Id = cs.AddressId OR csa.Id = cs.RemittanceId
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server-基于另一个表中的记录的SELECT记录 - SQL Server - SELECT records based on the records from another table 如何 select 记录在另一个表中没有链接记录是 null? - How to select records where no linked records in another table are null? 将包含大量记录的表导出到链接服务器上的另一个数据库 - exporting a table containing a large number of records to to another database on a linked server SQL Server:从一个表中为另一个表中的每条记录选择一个记录列表? - SQL Server: Select a list of records from a table for each record in another table? 如何从一个表中选择与SQL Server中的另一个表完全匹配的所有记录? - How to select all records from one table that exactly match another table in SQL Server? SQL 服务器 - 来自一个表的 select 记录不在另一个表的子集中 - SQL Server - select records from one table which are not in a subset of another table 从表SQL Server中选择数百万条记录 - select millions records from table sql server 改进具有许多记录的表中的SQL Server SELECT - Improve SQL Server SELECT in table with many records 在主表中查找与SQL Server中另一个表中的记录匹配的记录 - Finding records in main table that match records in another table in SQL Server SQL Server中的链接记录视图 - Linked records view in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM