我有2个表EnquiryMaster和Requirement。 我想从需求表中不存在enqiryid的表EnquiryMaster中删除重复的公司
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]
答案 0 :(得分: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
)
答案 1 :(得分:0)
从存在的EnquiryMaster E1中删除(从EnquiryMaster E2中选择1) 其中E1.Company = E2.Company和E1.EnquiryId&lt;&gt; E2.EnquiryId) 和E1.EnquiryId Not In(从需求中选择EnquiryId)