34,576
社区成员
发帖
与我相关
我的任务
分享
declare @t table
(id int,col1 varchar(8),col2 varchar(8),col3 varchar(7),col4 VARCHAR(5))
insert into @t
select 1,'fff','ggg','555',null union all
select 2,'7ullhio','wefcerte','9ihgyyu',null union all
select 1,NULL,'ggg','555',null union all
select 3,'55533399','8kkhy','20d9',null union all
select 4,'e','532','so0df','22888' union all
select 4,NULL,'wwi84','edxd','kd'
select * from @t
where id in(select id from @t group by id having(count(1)>1)) and col1 is null
/*
id col1 col2 col3 col4
----------- -------- -------- ------- -----
1 NULL ggg 555 NULL
4 NULL wwi84 edxd kd
*/
select *
from tb t
where exists (select 1 from tb where left(col,charindex(',',col)-1)=left(col,charindex(',',t.col)-1) and col <> t.col)
and left(substering(col,charindex(',',col),len(col)-charindex(',',col)+1),
charindex(',',left(substering(col,charindex(',',col),len(col)-charindex(',',col)+1))-1) = ''
select *
from tb t
where exists (select 1 from tb where left(col,charindex(',',col)-1)=left(col,charindex(',',t.col)-1) and col <> t.col)
and left(substering(col,charindex(',',t.col),len(col)-charindex(',',t.col)+1),
charindex(',',left(substering(col,charindex(',',t.col),len(col)-charindex(',',t.col)+1))-1) = ''