SQL fetches the last 10 rows in ascending order

here is my current code:

$messages = mysqli_query($link, "SELECT `user`, `message` FROM `chat` ORDER BY `id` DESC LIMIT 10");
while($row = mysqli_fetch_array($messages)) {
    echo "<strong>".$row['user'].":</strong> ".safe_out($row['message'])."<br />";  
}

      

Sends the last 10 messages from the table chat

in descending order. What I am trying to do is print the last 10 posts in ascending order.

Changing DESC

to ASC

just prints the first 10 messages, but I'm trying to get the last 10 messages to print in ascending order.

Do I need to output the mysqli_query results to an array and use reverse

or is there an easier way?

thank

+3


source to share


1 answer


You can use a derived table to re-sort the last 10 posts in ascending order



SELECT * FROM (
  SELECT `id`, `user`, `message` FROM `chat` ORDER BY `id` DESC LIMIT 10
) t1 ORDER BY t1.id

      

+3


source







All Articles