查字段指定数据后一行记录
/*
ID NUM
1 800
2 855
3 866
4 800
5 844
如何查NUM字段指定数据后一行记录?如NUM字段中800后一条记录 */
create table tb(ID int, NUM int)
insert into tb values(1 , 800 )
insert into tb values(2 , 855 )
insert into tb values(3 , 866 )
insert into tb values(4 , 800 )
insert into tb values(5 , 844 )
go
--如果ID连续
select m.* from tb m , tb n where m.id = n.id + 1 and n.num = 800
/*
ID NUM
----------- -----------
2 855
5 844
(所影响的行数为 2 行)
*/
--如果ID不连续,sql 2000
select m.id , m.num from
(
select t.* , px = (select count(1) from tb where id < t.id) + 1 from tb t
) m,
(
select t.* , px = (select count(1) from tb where id < t.id) + 1 from tb t
) n
where m.id = n.id + 1 and n.num = 800
/*
ID NUM
----------- -----------
2 855
5 844
(所影响的行数为 2 行)
*/
--如果ID不连续,sql 2005
select m.id , m.num from
(
select t.* , px = row_number() over(order by id) from tb t
) m,
(
select t.* , px = row_number() over(order by id) from tb t
) n
where m.id = n.id + 1 and n.num = 800
/*
ID NUM
----------- -----------
2 855
5 844
(所影响的行数为 2 行)
*/
drop table tb