如何从 table 交易代码中删除我有 child 而没有 parent 的交易代码?
How to delete from table tradecode where I have child without parent?
问题
如何从 table tradecode 中删除我有 childcodevalue 而没有 parentcodevalue 的行?
parentcodevalue 和 childcodevalue 存在于基于 table
的 table 交易代码中
MappingCodeValue parentcodevalue 和 childcodevalue .
所以我需要从交易代码 table 中删除没有 parent代码值
的记录
基于 table 的交易代码 table mappingcodevalue
所以根据我的解释,交易代码 table 上的两行 5,6 将被删除
TradeCodeId PartId CodeType CodeValue
5 1444 ECCS-URB AB666-URB
6 1931 ECCS-URB AB778-URB
5 和 6 是 childcodevalue 并且没有 parentcodevalue 行作为 AB666-US 和 AB778-US
所以错了,我会删除它
但贸易代码的另一行有 parent 代码值和 child 代码值
根据table映射code值所以是正确的
那么如何编写查询删除具有 childcodevalue 而没有
的行
parent来自贸易代码的代码值
基于映射代码值上存在的值
drop table #MappingCodeValue
drop table #TradeCode
create table #MappingCodeValue
(
id int identity (1,1),
ParentCodeType nvarchar(50),
ParentCodeValue nvarchar(50),
ChildCodeType nvarchar(50),
ChildCodeValue nvarchar(50)
)
INSERT INTO #MappingCodeValue
(ParentCodeType,ParentCodeValue,ChildCodeType,ChildCodeValue)
VALUES
('ECCS-US','AB123-US','ECCS-URB','AB123-URB'),
('ECCS-US','AB555-US','ECCS-URB','AB555-URB'),
('ECCS-US','AB666-US','ECCS-URB','AB666-URB'),
('ECCS-US','AB778-US','ECCS-URB','AB778-URB')
CREATE TABLE #TradeCode
(
TradeCodeId int identity(1,1),
PartId int,
CodeType nvarchar(50),
CodeValue nvarchar(50)
)
insert into #TradeCode(PartId,CodeType,CodeValue)VALUES
(1222,'ECCS-US','AB123-US'),
(1255,'ECCS-US','AB555-US'),
(1222,'ECCS-URB','AB123-URB'),
(1255,'ECCS-URB','AB555-URB'),
(1444,'ECCS-URB','AB666-URB'),
(1931,'ECCS-URB','AB778-URB')
我的尝试
delete t from #tradecode t
left join #mappingcodevalue pn on t.CodeValue=pn.ParentCodeValue and t.CodeType=pn.ParentCodeType
where t.CodeValue is null
查看图像修改
您正在使用 left join
,因此结果中不会有任何记录的左 table 列具有 NULL
值(您的 t
案例,因此,t.CodeValue
).
的记录将没有 NULL
您始终可以先检查不带 where
子句的记录,以查看删除不需要的记录的条件。
table#MAppingCodeValue
中不会有任何对应的记录,根据您的加入条件和table#TradeCode
中4条记录提供的示例数据(请参阅下面的屏幕截图)。
详情请查看db<>fiddle.
我在下面做这个查询并给我我需要的结果
delete from #TradeCode where PartId
not in (select t.PartId from #TradeCode t inner join #MappingCodeValue m
on t.CodeType = m.ParentCodeType and t.CodeValue = m.ParentCodeValue
where
(t.CodeValue = m.ParentCodeValue and t.CodeType = m.ParentCodeType) or
(t.CodeValue = m.ChildCodeValue and t.CodeType = m.ChildCodeType)
)
问题
如何从 table tradecode 中删除我有 childcodevalue 而没有 parentcodevalue 的行?
parentcodevalue 和 childcodevalue 存在于基于 table
的 table 交易代码中MappingCodeValue parentcodevalue 和 childcodevalue .
所以我需要从交易代码 table 中删除没有 parent代码值
的记录基于 table 的交易代码 table mappingcodevalue
所以根据我的解释,交易代码 table 上的两行 5,6 将被删除
TradeCodeId PartId CodeType CodeValue
5 1444 ECCS-URB AB666-URB
6 1931 ECCS-URB AB778-URB
5 和 6 是 childcodevalue 并且没有 parentcodevalue 行作为 AB666-US 和 AB778-US
所以错了,我会删除它
但贸易代码的另一行有 parent 代码值和 child 代码值
根据table映射code值所以是正确的
那么如何编写查询删除具有 childcodevalue 而没有
的行parent来自贸易代码的代码值
基于映射代码值上存在的值
drop table #MappingCodeValue
drop table #TradeCode
create table #MappingCodeValue
(
id int identity (1,1),
ParentCodeType nvarchar(50),
ParentCodeValue nvarchar(50),
ChildCodeType nvarchar(50),
ChildCodeValue nvarchar(50)
)
INSERT INTO #MappingCodeValue
(ParentCodeType,ParentCodeValue,ChildCodeType,ChildCodeValue)
VALUES
('ECCS-US','AB123-US','ECCS-URB','AB123-URB'),
('ECCS-US','AB555-US','ECCS-URB','AB555-URB'),
('ECCS-US','AB666-US','ECCS-URB','AB666-URB'),
('ECCS-US','AB778-US','ECCS-URB','AB778-URB')
CREATE TABLE #TradeCode
(
TradeCodeId int identity(1,1),
PartId int,
CodeType nvarchar(50),
CodeValue nvarchar(50)
)
insert into #TradeCode(PartId,CodeType,CodeValue)VALUES
(1222,'ECCS-US','AB123-US'),
(1255,'ECCS-US','AB555-US'),
(1222,'ECCS-URB','AB123-URB'),
(1255,'ECCS-URB','AB555-URB'),
(1444,'ECCS-URB','AB666-URB'),
(1931,'ECCS-URB','AB778-URB')
我的尝试
delete t from #tradecode t
left join #mappingcodevalue pn on t.CodeValue=pn.ParentCodeValue and t.CodeType=pn.ParentCodeType
where t.CodeValue is null
查看图像修改
您正在使用 left join
,因此结果中不会有任何记录的左 table 列具有 NULL
值(您的 t
案例,因此,t.CodeValue
).
NULL
您始终可以先检查不带 where
子句的记录,以查看删除不需要的记录的条件。
table#MAppingCodeValue
中不会有任何对应的记录,根据您的加入条件和table#TradeCode
中4条记录提供的示例数据(请参阅下面的屏幕截图)。
详情请查看db<>fiddle.
我在下面做这个查询并给我我需要的结果
delete from #TradeCode where PartId
not in (select t.PartId from #TradeCode t inner join #MappingCodeValue m
on t.CodeType = m.ParentCodeType and t.CodeValue = m.ParentCodeValue
where
(t.CodeValue = m.ParentCodeValue and t.CodeType = m.ParentCodeType) or
(t.CodeValue = m.ChildCodeValue and t.CodeType = m.ChildCodeType)
)