How to determine the order data read from the MYSQL database?

Is there a way to change the order data from the database? you know, the default order is that they are read from the first data inserted last, so is there a way to change it to last-> first?

+3
source share
5 answers

the default order is that they are read from the first data inserted in the last

, . , . ORDER BY, . , , , .

, order by. , , insert_date, NOW() :

ORDER BY `insert_date` DESC

, , .

+5

. , Engine Engine.

, , InnoDB, PRIMARY KEY, MyISAM

+3

( , ) ORDER BY DESC.

. ORDER BY , .

+1

: "ORDER BY" SQL-, timestamp, / .

(ORDER BY myTimestampField DESC)

+1
source

SELECT * FROM {tablename} ORDER BY id DESC

The above assumes that you have an identifier field that grows with each insertion. You may also have a field of type CreateDate, which you can use in the ORDER BY statement.

ORDER BY will be ascending by default, so in order to get-> first, use DESC.

+1
source

Source: https://habr.com/ru/post/1746201/


All Articles