简体   繁体   English

如何查看 SQL 表中的最近记录?

[英]How do i view recent records from SQL table?

I have one table called "Mydata" with date,name,address columns this table is having more than 5000 records now i dont want to view all 5000 records I just want to view recently added 100 records, how can i do that I tried with select top 100 * from Mydata But which is not giving me recently added records我有一个名为“Mydata”的表,其中包含日期、名称、地址列,该表现在有超过 5000 条记录我不想查看所有 5000 条记录我只想查看最近添加的 100 条记录,我该怎么做? select 前 100 * 来自 Mydata 但这并没有给我最近添加的记录

For TOP 100 * to work as you desire, you need to specify the order in which to return the records.要使TOP 100 *按您的意愿工作,您需要指定返回记录的顺序。 Use the example below and replace [YourField] with whatever column determines the order in which the records were inserted.使用下面的示例并将[YourField]替换为确定插入记录顺序的任何列。

SELECT TOP 100 *
FROM       Mydata
ORDER BY   [YourField] DESC --Could be the date added column, or the primary key.

The ordering of the data is not guaranteed.不保证数据的顺序。 Well that's not entirely correct as you can cluster there data by a field.好吧,这并不完全正确,因为您可以按字段对数据进行聚类。 However unless put the insertion data an time on the row in a field, you can't query by that.但是,除非将插入数据放在字段中的行上,否则您无法查询。

So add a column (say called insdate) to your table what puts the current date and time into the row when you insert it.因此,在您的表中添加一列(例如称为 insdate),当您插入它时,它会将当前日期和时间放入行中。 Then query by date time ordering然后按日期时间排序查询

select top 100 * from mydata order by insdate desc

Like james hill says, you can use any column to indicate the ordering if the value is unique and changes in the same way.就像詹姆斯希尔所说,如果值是唯一的并且以相同的方式变化,您可以使用任何列来指示排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM