I just want to select the newest 3 comments on a post, and have them ordered in ASC order.
我只想在帖子上选择最新的3条评论,并按照ASC顺序订购。
This selects the last 3 rows, however I need them in the reverse order:
这会选择最后3行,但我需要它们以相反的顺序:
mysql_query("
SELECT * FROM comments WHERE postID='$id' AND state='0' ORDER BY id DESC LIMIT 3")
3 个解决方案
#1
15
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;
#2
2
This can also be done just in PHP, without modifying the SQL query, by simply iterating backwards through the result set:
这也可以在PHP中完成,无需修改SQL查询,只需向后迭代结果集即可:
$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.
我承认我不知道性能差异是什么(如果有的话),但它只是可能适合你的另一种选择。
#3
-1
$result = mysqli_query($con,"SELECT * FROM (SELECT * FROM messeges WHERE username='{$_SESSION['username']}' ORDER BY id DESC LIMIT 3) t ORDER BY id ASC");
#1
15
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;
#2
2
This can also be done just in PHP, without modifying the SQL query, by simply iterating backwards through the result set:
这也可以在PHP中完成,无需修改SQL查询,只需向后迭代结果集即可:
$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.
我承认我不知道性能差异是什么(如果有的话),但它只是可能适合你的另一种选择。
#3
-1
$result = mysqli_query($con,"SELECT * FROM (SELECT * FROM messeges WHERE username='{$_SESSION['username']}' ORDER BY id DESC LIMIT 3) t ORDER BY id ASC");