简体   繁体   中英

How to get most recent row in a table?

On my front page I would like my most recent Blog post or message to be displayed.

My current query however displays the messages in order of row number so the oldest or first blog message is displayed and the newest is at the bottom of the page.

I would like it so the most recent 5 blogs are displayed.

In my table "blog" I have columns:

-blog_id

-title

-author

-date (ymd)

-content

This is my query

SELECT * FROM blog LIMIT 5

I tried using SELECT MAX but it only returns one row.

How can I get the last 5 rows etc.

I would like to order it by row_id (blog_id) as some posts may have the same date.

Thanks

http://dev.mysql.com/doc/refman/5.5/en/sorting-rows.html

SELECT * FROM blog ORDER BY date DESC,blog_id DESC LIMIT 5

SELECT * FROM blog ORDER BY date DESC LIMIT 5

Assuming your blog IDs are serial, this should work:

SELECT * FROM blog ORDER BY blog_id DESC LIMIT 5

Note that you can't sort by date because it doesn't include the time - if two blogs were created on the same day you don't know which was created later from the date.

SELECT * FROM `blog` ORDER BY `blog_id` DESC LIMIT 5

这将选择最新的5篇博客文章,从最新的一篇开始。

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