Mysql Select Second Row

前端 未结 4 1925
既然无缘
既然无缘 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.

    0 讨论(0)
  • 2021-02-12 18:44

    SELECT * FROM nieuws ORDER BY id DESC LIMIT 2 - selects last 2 items

    SELECT * FROM nieuws ORDER BY id DESC LIMIT 1, 1 - selects only second item

    0 讨论(0)
  • 2021-02-12 18:59

    LIMIT can take two arguments:

    SELECT ... LIMIT 1, 1
    
    0 讨论(0)
  • 2021-02-12 19:00

    SELECT * FROM table where id >(SELECT id FROM table order by id ASC limit1,1) and id <=(select max(id) from table)

    0 讨论(0)
提交回复
热议问题