这个怎么用查询简单实现?

gahade 2006-08-25 04:05:50
id num
------------------------------
1 10
2 11
3 22
4 23
5 41
6 44

找出id为1,3,5这3条,条件为11-10=1,23-22=1,44-41=3,范围在3以内的!
谢谢
...全文
174 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
$扫地僧$ 2006-08-25
  • 打赏
  • 举报
回复
create table #t(id int identity(1,1),num int)
insert into #t(num)
select 10
union all select 11
union all select 22
union all select 23
union all select 41
union all select 44


select T1.id
from
(select * from #t where id%2>0) T1,
(select * from #t where id%2=0) T2
where T1.id=T2.id-1
and T2.num-T1.num <=3
wisdomone 2006-08-25
  • 打赏
  • 举报
回复
u
dba_sunny 2006-08-25
  • 打赏
  • 举报
回复
create table #t(id int identity(1,1),num int)
insert into #t(num)
select 10
union all select 11
union all select 22
union all select 23
union all select 41
union all select 44

select * from #t as a
where exists(select * from #t where (num-a.num) between 1 and 3)
dba_sunny 2006-08-25
  • 打赏
  • 举报
回复
create table #t(id int identity(1,1),num int)
insert into #t(num)
select 10
union all select 11
union all select 22
union all select 23
union all select 41
union all select 44

select * into #t2 from #t

select #t.*
from #t,#t2
where #t.id=#t2.id and ((select num from #t2 where #t.id=#t2.id+1) -#t.num)<3
dba_sunny 2006-08-25
  • 打赏
  • 举报
回复
declare #t table (id int identity(1,1),num int)
insert into #t(num)
select 10
union all select 11
union all select 22
union all select 23
union all select 41
union all select 44

select * into #t2 from #t

select #t.*
from #t,#t2
where #t.id=#t2.id and ((select num from #t2 where #t.id=#t2.id+1) -#t.num)<3
WangZWang 2006-08-25
  • 打赏
  • 举报
回复
declare @t table (id int identity(1,1),num int)
insert into @t(num)
select 10
union all select 11
union all select 22
union all select 23
union all select 41
union all select 44

select * from @t

select * from @t as a
where exists(select * from @t where (num-a.num) between 1 and 3)
gahade 2006-08-25
  • 打赏
  • 举报
回复
知道了,加上a.id < #t.id
gahade 2006-08-25
  • 打赏
  • 举报
回复
To WangZWang(先来):
不是正确的结果啊

create table #t(id int identity(1,1),num int)
insert into #t(num)
select 10
union all select 11
union all select 22
union all select 23
union all select 41
union all select 44

select * from #t as a
where exists(select * from #t where abs(num-a.num) between 1 and 3)

drop table #t
WangZWang 2006-08-25
  • 打赏
  • 举报
回复
select * from tb as a
where exists(select * from tb where abs(num-a.num) between 1 and 3)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧