34,576
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE #A (name char(1), value int)
GO
INSERT into #A values('A', 1)
INSERT into #A values('B', 2)
INSERT into #A values('C', 5)
INSERT into #A values('D', 9)
INSERT into #A values('E', 10)
INSERT into #A values('F', 10)
GO
select distinct a.* from #A a left join #A b on abs(a.value-b.value)<=2 where a.name<>b.name and a.value<b.value
--or
select * from #A a where exists(select 1 from #A b where a.name<>b.name and a.value<b.value and abs(a.value-b.value)<=2)
/**
name value
---- -----------
A 1
D 9
(所影响的行数为 2 行)
**/