I just want to select the newest 3 comments on a post, and have them ordered in ASC order.
This selects the last 3 rows, however I need them in the reverse order:
mysql_query("
SELECT * FROM comments WHERE postID='$id' AND state='0' ORDER BY id DESC LIMIT 3")
You can reverse sort it later.
SELECT *
FROM (SELECT * FROM comments
WHERE postID='$id'
AND state='0'
ORDER BY id DESC
LIMIT 3) t
ORDER BY id ASC;
This can also be done just in PHP, without modifying the SQL query, by simply iterating backwards through the result set:
$res = mysql_query(...);
for($i=mysql_num_rows($res)-1; $i>=0; $i--) {
//do whatever
}
I confess I don't know what the performance difference is (if any), but it's just another option that might suite you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With