Chris
Chris

Reputation: 461

MySQL query for ID < variable

Hey, so what I am looking to accomplish is a MySQL query which grabs just the FIRST row whos ID is less than my current one. So basically a next button. Here is what I would like to modify:

$next = mysql_query("select * from posts where id<'$id'");

So how would I change this so it only selects the first value which is less than my ID value (which is the id of my current page)

Thanks! I will be doing this the reverse way for a previous button too.

Upvotes: 4

Views: 3067

Answers (2)

AbiusX
AbiusX

Reputation: 2404

$next=mysql_query("SELECT * FROM posts WHERE id<'{$id}' ORDER BY id LIMIT 1");

Upvotes: 2

Femaref
Femaref

Reputation: 61437

select * from posts where id<'$id' order by id desc limit 1

This will get you the first id smaller than the provided parameter $id.

Upvotes: 5

Related Questions