John Smith
John Smith

Reputation: 8811

Is it possible to retrieve every record after one with a certain ID?

Let's say I'm using MySQL and want to sort a table alphabetically by the column 'name' and then retrieve all records after a record whose 'id' is, say, 10.

Can this be done? What would the query look like?

Upvotes: 0

Views: 76

Answers (2)

martriay
martriay

Reputation: 5742

If id is the PK...

SELECT * FROM 'mytable' WHERE 'id'>10 ORDER BY 'name'

according to nathan's interpretation of the question...

ALTER TABLE 'mytable' ORDER BY 'name';
SELECT * FROM 'mytable' WHERE 'id'>10;

According to OP's requirements:

$query = "SELECT * FROM 'mytable' ORDER BY 'name'";
$result = mysql_fetch_assoc(mysql_query($query, $connection));

while($row = $result) {
  if ($row['id'] != 10) {
    array_shift($result);
  } else {
    break;
  }
}

then $result is your final response :)

Upvotes: 0

Fabian Blechschmidt
Fabian Blechschmidt

Reputation: 4141

What about a subquery?

SELECT * FROM `mytable`
    WHERE `name` > (SELECT name FROM `mytable` WHERE `id` = 10)
    ORDER BY `name`

Upvotes: 1

Related Questions