简体   繁体   中英

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

For TOP 100 * to work as you desire, you need to specify the order in which to return the records. Use the example below and replace [YourField] with whatever column determines the order in which the records were inserted.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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