如何在sql中获得最后输入的行? [英] How to get last entered row in sql?

查看:115
本文介绍了如何在sql中获得最后输入的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个名为详细信息的表,其中包含名称列。此列包含许多已在表中输入的值。目前,我已在名称列中输入了一些值。我需要显示最后输入的10个值。我们在sql中有一个查询来显示第一个值top(10)值。喜欢我需要底部值(最后输入的值)。

解决方案

你可以使用order by子句做这样的事情: -



按名称desc从Tbl_Name顺序中选择前10 * 


为了显示top n 值(或底部n值)你必须专门对行应用一个订单,因为否则SQL可以自由地返回任何适合它的行 - 这可能是最后输入的顺序但是不必是 - 也不必连续​​两次同一个订单。



所以,最好的方法是添加一个时间戳 你的数据库的字段,所以你把你输入的日期和时间放入行。

然后它很简单:

 < span class =code-keyword> SELECT   TOP   10  *  FROM  MyTable  ORDER   BY  EnterDate  DESC  


试试这个.. :)





 选择  top   10  *  from  TableName  order   by  id 降序 


Hi,
I have a table named "Details" with column "Name". This column contains lots of values which are already been entered in the table. Currently I have entered some values to the column "Name". I need to show last entered 10 values. We have a query in sql to show first values top(10) values. Like to that I need bottom values(last entered values).

解决方案

you can do something like this using order by clause:-

select top 10 * from Tbl_Name order by Name desc


In order to show the "top n" values (or the "bottom n" values for that matter) you have to specifically apply an order to the rows, becuase otherwise SQL is at liberty to return rows an any order which suits it - which may be "last entered order" but does not have to be - nor does it have to be the same order twice in a row.

So, the best way to do this is to add a "timestamp" field to your database, so that you put the date and time you entered them into the row.
then it's simple:

SELECT TOP 10 * FROM MyTable ORDER BY EnterDate DESC


try this.. :)


select top 10 * from TableName order by id desc


这篇关于如何在sql中获得最后输入的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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