如何从表中检索最后两个已记录 [英] How to retrive last two recoed from table
本文介绍了如何从表中检索最后两个已记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
先生您好,
我想从该表中检索最后两个记录.该怎么办?
id 城市 州
18 斋浦尔 拉贾斯坦邦
38 孟买 马哈拉施特拉邦
22 艾哈迈达巴德古吉拉特邦
21 苏拉特 古吉拉特邦
25 浦那
19 asd
Outpu应该是..
25 浦那
19 asd
在此先谢谢您
Hello sir,
I want to retrive last two record from this table.How can i do it?
id city state
18 Jaipur Rajasthan
38 Mumbai Maharashtra
22 Ahmedabad Gujarat
21 Surat Gujarat
25 Pune Maharashtra
19 asd asd
Outpu Should be..
25 Pune Maharashtra
19 asd asd
thanks in advance
推荐答案
您所要求的甚至不是按照ID的顺序. AFAIK,您将必须声明某种ORDER BY才能获取最后两个"记录. SQL没有数据顺序的概念.
What you ask is not even in order as per ID. AFAIK, you will have to declare some sort of ORDER BY to get the "last two" records. SQL has no concept of an order to the data.
SQL中的数据插入页面中,除非从页面上删除了数据,否则新数据通常会到达最后一页/当前页面.新行的可用空间.
因此,除非您明确使用某种可以递增并可以排序的记录号,否则无法保证数据将按插入顺序进行.
Data in SQL are inserted in pages, new data generally goes to the last/current page unless data was deleted from a page hence the availability of free space for new rows.
So there is no guarantee that data will be in insert order unless you explicitly use some kind of record number which will increment and can be sorted.
您可以尝试-
You can try-
SELECT * FROM table_name Where id not in (
SELECT top (SELECT COUNT(*)-2 FROM table_name) id FROM table_name )
祝你好运....
best of luck ....
这篇关于如何从表中检索最后两个已记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文