34,587
社区成员
发帖
与我相关
我的任务
分享
declare @tmpTable table([id] int,num int identity)
declare @tmpnum int
insert @tmpTable([id])
Select [id] From tb1 Order by SortName
select @tmpnum = num
from @tmpTable
where [id] = 2
select max([id])
from @tmpTable
where num < @tmpnum
select min([id])
from @tmpTable
where num > @tmpnum
declare @tb1 table (id int,sortName varchar(10),field1 varchar(20))
insert into @tb1 select 1,'D','aaaa'
insert into @tb1 select 2,'C','adsfasd'
insert into @tb1 select 3,'A','asdfasd'
insert into @tb1 select 4,'B','adsfasd'
select isnull((select max(id) from @tb1 where id < t.id),t.id) as prevId,
t.id,
isnull((select min(id) from @tb1 where id > t.id),t.id) as NextId
from @tb1 t
/*
prevId id NextId
----------- ----------- -----------
1 1 2
1 2 3
2 3 4
3 4 4
*/
create table tb1(id int,sortName varchar(10),field1 varchar(20))
insert into tb1 select 1,'D','aaaa'
insert into tb1 select 2,'C','adsfasd'
insert into tb1 select 3,'A','asdfasd'
insert into tb1 select 4,'B','adsfasd'
select maxid=(select top 1 id from tb1 where id>a.id),id,
minid=(select top 1 id from tb1 where id<a.id order by id desc) from tb1 a
declare @tmpTable table([id] int,num int identity)
declare @tmpnum int
insert @tmpTable([id])
Select [id] From tb1 Order by SortName
select @tmpnum = num
from @tmpTable
where [id] = 2
select max([id])
from @tmpTable
where num < @tmpnum
select min([id])
from @tmpTable
where num > @tmpnum
select top 1 * from (select * from t1 where [name]<'myname') a order by [name] desc
select top 1 * from (select * from t1 where [name]>'myname') a order by [name] desc
create table tb1(id int,sortName varchar(10),field1 varchar(20))
insert into tb1 select 1,'D','aaaa'
insert into tb1 select 2,'C','adsfasd'
insert into tb1 select 3,'A','asdfasd'
insert into tb1 select 4,'B','adsfasd'
insert into tb1 select 5,'C','adsfasd'
insert into tb1 select 10,'A','asdfasd'
insert into tb1 select 12,'B','adsfasd'
declare @id int
declare @ids varchar(8000)
select @ids=isnull(@ids+',','')+ltrim(id) from tb1
set @id=5
select
previd=case when charindex(','+ltrim(@id)+',',','+@ids+',')=1 then left(reverse(@ids),charindex(',',reverse(@ids))-1)
else reverse(left(reverse(left(@ids,charindex(','+ltrim(@id)+',',','+@ids+',')-2))+',',charindex(',',reverse(left(@ids,charindex(','+ltrim(@id)+',',','+@ids+',')-2))+',')-1)) end,
nextid=case when charindex(','+ltrim(@id)+',',','+@ids+',')+len(@id)-1=len(@ids) then left(@ids,charindex(',',@ids)-1)
else substring(substring(@ids,charindex(','+ltrim(@id)+',',','+@ids+',')+len(@id)+1,len(@ids))+',',0,charindex(',',substring(@ids,charindex(','+ltrim(@id)+',',','+@ids+',')+len(@id)+1,len(@ids))+',')) end
declare @tb1 table (id int,sortName varchar(10),field1 varchar(20))
insert into @tb1 select 3,'A','asdfasd'
insert into @tb1 select 4,'B','adsfasd'
insert into @tb1 select 2,'C','adsfasd'
insert into @tb1 select 1,'D','aaaa'
select isnull((select top 1 id from @tb1 where sortName < t.sortName order by sortName desc),t.id) as prevId,
t.id,
isnull((select top 1 id from @tb1 where sortName > t.sortName order by sortName asc),t.id) as NextId
from @tb1 t
/*
prevId id NextId
----------- ----------- -----------
3 3 4
3 4 2
4 2 1
2 1 1
*/