Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse order output of a MySQL query

Tags:

sql

php

mysql

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 the entry before that, then the entry before that, etc. The first entry ever is last.

like image 1000
Jacob David C. Cunningham Avatar asked Sep 21 '13 12:09

Jacob David C. Cunningham


People also ask

How do I reverse a SQL query?

The REVERSE() function reverses a string and returns the result.

In which order does MySQL sort your output?

The MySQL ORDER BY Keyword The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

How do I roll back MySQL?

To roll back the current transaction and cancel its changes, you use the ROLLBACK statement. To disable or enable the auto-commit mode for the current transaction, you use the SET autocommit statement.

How do I undo an action in MySQL?

At any point, you may undo all of the statements for the transaction by entering the following SQL statements: ROLLBACK; 5.


3 Answers

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.

like image 173
user2801966 Avatar answered Sep 25 '22 06:09

user2801966


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
like image 44
Oswald Avatar answered Sep 23 '22 06:09

Oswald


Sort using DESC ORDER BY.

SELECT * FROM <TABLE> ORDER BY <COLUMN> DESC
like image 29
Ashwini Agarwal Avatar answered Sep 23 '22 06:09

Ashwini Agarwal