Mysql Select Second Row

前端 未结 4 1927
既然无缘
既然无缘 2021-02-12 18:29

I have a mysql question.


I have a news section on my website, and I want to display the two latest items. If I do:

SELECT * FROM nieuws ORDER BY id         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-12 18:39

    If you want to display the latest two items, then you can get both at the same time by limiting to 2 instead of 1. This means it's only one database hit to get the information you need.

    SELECT * FROM nieuws ORDER BY id DESC LIMIT 2
    

    Or if you only want the second row, you can give an offset to the LIMIT, to tell it which row to start from, (Although if you get the first row in one query, then get the second in another, you're doing two database hits to get the data you want, which can affect performance).

    SELECT * FROM nieuws ORDER BY id DESC LIMIT 1, 1
    

    You can find out more information on how to use the LIMIT clause in the MySQL documentation.

提交回复
热议问题