如何在SQL中选择上一行 [英] how to select previous row in sql
本文介绍了如何在SQL中选择上一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在sql中选择上一行
例如:从给定表中
how to select previous row in sql
eg:from the given table
id name
1 nn
3 mm
7 ji
如果我选择ID 7,我想得到mm
如果我选择3
i wanna get mm if i select the id 7
i want to get nn if i select 3
推荐答案
select top 1 id,name from tbl where id<7 order by id desc
祝您编码愉快!
:)
Happy Coding!
:)
create table temp_Teable
(id int,
val varchar(100))
insert into temp_teable values (1,'mm')
insert into temp_teable values (3,'nn')
insert into temp_teable values (7,'rr')
insert into temp_teable values (16,'rrr')
insert into temp_teable values (8,'rrrr')
with cte_test as
(select id,val,row_number() over (order by (select 0)) as rowno from temp_Teable)
Select t1.val from cte_test t1
inner join cte_test t2 on t1.rowno=t2.rowno-1
where t2.id=8
这篇关于如何在SQL中选择上一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文