asp读数据

来源:百度知道 编辑:UC知道 时间:2024/07/02 18:10:22
要读数据库中倒数第二条数据怎么写代码?

select top 1 * from (select top 2 * from 表 order by id desc) tmp order by id asc

方法1:
sql="select top 1 * from 表 where id not in(select top 1 id from 表) order by id desc"

方法2:
sql="select top 1 * from 表 where id in(select top 2 id from 表 order by id desc) order by id asc"

select top 1 * from 表 where id in (select top 2 id from 表 order by id desc) order by id asc

括号里取的是最后2条记录的id,如果你的记录数目是100,这取的是第99和100二条。然后取这2条记录的第一条,也就是第99条。