
April 24th, 2008, 06:49 PM
|
|
Contributing User
|
|
Join Date: Jun 2004
Posts: 149
Time spent in forums: 2 Days 13 h 44 m 30 sec
Reputation Power: 5
|
|
|
Selecting Previous & Next Record based off Ordering column
I'm trying to select the ID of the previous and next record based off of an column that contains the ORDER. I pass the SELECT statement an ID of a record and it is suppose to return the previous record ID and the next record ID. The SELECT statement I'm current using works in most cases, but it is flawed and I'm not seeing why. Any help would be appreciated.
PHP Code:
$sql = "SELECT id,
(SELECT MAX(id) FROM content AS ma WHERE ma.ordering < content.ordering) AS prev_row,
(SELECT MIN(id) FROM content AS mi WHERE mi.ordering > content.ordering) AS next_row
FROM content WHERE id = " . $db->qstr($id);
|