如何检索倒数第二行? [英] How can I retrieve second last row?

查看:43
本文介绍了如何检索倒数第二行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多记录的表,我只想知道我在倒数第二个创建的记录.

I have a table with many records and I want to know only the record which I have created at second last.

例如:我有一个表 customer,其中 customerID 是随机数.

For ex: I have a table customer in which customerID are random numbers.

现在我想选择倒数第二行.

customerID      customer_name   cont_no
---------------------------------------
 7              david sam       5284
 1              shinthol        1
11              lava            12548
 2              thomas          1
 3              peeter          1
 4              magge           1
 5              revas           1
 6              leela           123975

输出行:

customerID      customer_name   cont_no
5               revas           1

我不想第二高...

我想要倒数第二行.

推荐答案

如你所问,我可以举个例子.

As you asked I can give you example.

想象一下,你有满满一袋苹果.你怎么能吃倒数第二个苹果?你怎么知道哪个是倒数第二?如果您不以任何方式对它们进行排序,您就无法做到这一点.

Imagine, that you have full bag of apples. How can you take second last apple? How you will know which one is second last? You can't do It while you not sort them in any way.

目前您的数据尚未排序,因此您无法按预期实现它.您可以按照以下方式进行操作,前提是您有任何排序条件,例如 Iddate created 等.

For now your data isn't sorted so you can't achieve It as expected. You can do It in following, only after you have any sorting criteria like Id, date created or etc.

SELECT TOP 1 * 
FROM(
    SELECT TOP 2 * 
    FROM Tbl 
    ORDER BY SortingCol DESC -- here you need to pass column which will provide expected sorting
    ) t                     
ORDER BY SortingCol

这篇关于如何检索倒数第二行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆