22,209
社区成员
发帖
与我相关
我的任务
分享
declare @tb table(SN varchar(10),ParentSN varchar(10))
insert into @tb values('001 ','0')
insert into @tb values('0011','001')
insert into @tb values('0012','001')
insert into @tb values('002 ','0')
insert into @tb values('0021','002')
insert into @tb values('0022','002')
select * from @tb where sn not in (select ParentSN from @tb)
/*
0011 001
0012 001
0021 002
0022 002
*/
-- ParentSN 有 null 值
declare @tb table(SN varchar(10),ParentSN varchar(10))
insert into @tb values('001 ',NULL)
insert into @tb values('0011','001')
insert into @tb values('0012','001')
insert into @tb values('002 ','0')
insert into @tb values('0021','002')
insert into @tb values('0022','002')
select * from @tb where sn not in (select ParentSN from @tb)
/* */
select * from @tb t where not exists (select 1 from @tb where t.sn=ParentSN)
/*
0011 001
0012 001
0021 002
0022 002
*/
--这样?
select
*
from
V1_GongZuoBiaoZhunJiBenBiao t
where
not exists(select 1 from V1_GongZuoBiaoZhunJiBenBiao where parentsn=t.sn)
create table tb(SN varchar(10), ParentSN varchar(10))
insert into tb values('001 ' , '0')
insert into tb values('0011' , '001')
insert into tb values('0012' , '001')
insert into tb values('002 ' , '0')
insert into tb values('0021' , '002')
insert into tb values('0022' , '002')
go
select m.* from tb m where not exists(select 1 from tb n where ParentSN = m.sn)
drop table tb
/*
SN ParentSN
---------- ----------
0011 001
0012 001
0021 002
0022 002
(所影响的行数为 4 行)
*/
select * from V1_GongZuoBiaoZhunJiBenBiao a
where not exists(select 1 from V1_GongZuoBiaoZhunJiBenBiao where parentsn=a.sn)