Skip to content Skip to sidebar Skip to footer

How To Reverse Order Output Of A MySQL Query

I have a basic write to and retrieve SQL database PHP 'thing' and I want to have the output in descending order. How do I do that? For example, the last entry is shown first, then

Solution 1:

Use:

SELECT field_name
FROM table_name
ORDER BY id DESC

By default, MySQL will show results in ascending order. If you want to show them in reverse order, use ORDER BY field_name DESC.

You can use id or date as the field name.


Solution 2:

Sort using DESC ORDER BY.

SELECT * FROM <TABLE> ORDER BY <COLUMN> DESC

Solution 3:

Change the ORDER BY statement

  • from ORDER BY col or ORDER BY col ASC or to ORDER BY col DESC
  • from ORDER BY col DESC to ORDER BY col ASC

Solution 4:

If there's an auto increment field, you order by that field, descending.

SELECT "column_name"
  FROM "table_name"
  [WHERE "condition"]
  ORDER BY "column_name" [ASC, DESC]; 

That's from ORDER BY Clause - Sort Data In SQL.


Solution 5:

Write the below query to retrieve data:

SELECT * FROM `table_name` order by id desc

It will retrieve data from the table in descending order.


Post a Comment for "How To Reverse Order Output Of A MySQL Query"