如何从表中检索最后2条记录?(How to retrieve the last 2 records from table?)

编程入门 行业动态 更新时间:2024-10-15 16:23:42
如何从表中检索最后2条记录?(How to retrieve the last 2 records from table?)

我有一张有n个记录的表格

如何从SQL中的表中检索第n条记录和第(n-1)条记录而不使用派生表?

我曾尝试使用ROWID作为

select * from table where rowid in (select max(rowid) from table);

它给出了第n个记录,但我也想要第(n-1)条记录。 除了使用max,derived table and pseudo columns之外max,derived table and pseudo columns还有其他方法max,derived table and pseudo columns

谢谢

I have a table with n number of records

How can i retrieve the nth record and (n-1)th record from my table in SQL without using derived table ?

I have tried using ROWID as

select * from table where rowid in (select max(rowid) from table);

It is giving the nth record but i want the (n-1)th record also . And is there any other method other than using max,derived table and pseudo columns

Thanks

最满意答案

select * from (select * from my_table order by rowid) where rownum <= 2

对于N和M之间的行:

select * from ( select * from ( select * from my_table order by rowid ) where rownum <= M ) where rownum >= N select * from (select * from my_table order by rowid) where rownum <= 2

and for rows between N and M:

select * from ( select * from ( select * from my_table order by rowid ) where rownum <= M ) where rownum >= N

更多推荐

本文发布于:2023-08-06 13:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1449383.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:retrieve   records   table

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!