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
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.
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
LIMIT
can take two arguments:
SELECT ... LIMIT 1, 1
SELECT * FROM table where id >(SELECT id FROM table order by id ASC limit1,1) and id <=(select max(id) from table)