3D-kreativ
3D-kreativ

Reputation: 9297

How to sort in MySQL

In the code below, I get the 9 latest objects in the table. Of this 9 objects, the last is presented as the latest. I wonder if it's possible to get the oppesite. Of this 9 objects, that the latest is the first in the result!? I hope my question isn't unclear!? It's hard to explain!

$query2 = "SELECT * from buildingObjects WHERE id > (SELECT MAX(id) - 9 FROM arkitekturobjekt)";
$result = $mysqli->query($query2);
while($row = $result->fetch_object()) {
// Show the objects here
}

Upvotes: 0

Views: 62

Answers (2)

ErickBest
ErickBest

Reputation: 4692

If id is an auto_increment primary key index you can go for ORDER BY id DESC, still if you are also storing dates or date and time, you could go for the date field order instead.

Upvotes: 0

Jacob S
Jacob S

Reputation: 1703

If you are only interested in the last 9 IDs, listed last to first, you could simply do:

SELECT * FROM buildingObjects ORDER BY id DESC LIMIT 9

Upvotes: 2

Related Questions