0

我有 2 个表 EnquiryMaster 和 Requirement。我想从表 EnquiryMaster 中删除重复的 Company,其 enqiryid 在 Requirement 表中不存在

  CREATE TABLE [dbo].[EnquiryMaster](
[EnquiryId] [int] IDENTITY(1,1) NOT NULL,
[orgid] [int] NULL,
[uid] [int] NULL,
[Company] [varchar](160) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[HeadOfficeAddress] [varchar](460) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_EnquiryMaster] PRIMARY KEY CLUSTERED 
    (
[EnquiryId] ASC
     )WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]


    CREATE TABLE [dbo].[Requirement](
[RequirementID] [int] IDENTITY(1,1) NOT NULL,
[uid] [int] NULL,
[orgid] [int] NULL,
[EnquiryID] [int] NULL,
[Company] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

    CONSTRAINT [PK_Requirement] PRIMARY KEY CLUSTERED 
     (
[RequirementID] ASC
     )WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
     ) ON [PRIMARY]
4

2 回答 2

0
DELETE FROM [dbo].[EnquiryMaster]
WHERE [EnquiryId] NOT IN 
(SELECT [EnquiryID] FROM [dbo].[Requirement])

但不清楚为什么将这些行称为重复行。

UPD。考虑重复。

DELETE T1
FROM dbo.EnquiryMaster T1
WHERE EXISTS (
    SELECT 1
    FROM dbo.EnquiryMaster T2
    WHERE T1.Company = T2.Company AND T1.EnquiryId <> T2.EnquiryId
) AND T1.EnquiryId NOT IN (
    SELECT EnquiryID
    FROM dbo.Requirement
)
于 2013-10-23T09:59:58.690 回答
0

从存在的 EnquiryMaster E1 中删除(从 EnquiryMaster E2 中选择 1,其中 E1.Company = E2.Company 和 E1.EnquiryId<>E2.EnquiryId)和 E1.EnquiryId 不在(从需求中选择 EnquiryId)

于 2013-10-23T12:22:12.350 回答