我有一个基本的写入和检索 SQL 数据库 PHP“东西”,我希望输出按降序排列。我怎么做?
例如,最后一个条目首先显示,然后是之前的条目,然后是之前的条目,等等。第一个条目是最后一个。
利用:
SELECT field_name
FROM table_name
ORDER BY id DESC
默认情况下,MySQL 将按升序显示结果。如果您想以相反的顺序显示它们,请使用ORDER BY field_name DESC
.
您可以使用 id 或 date 作为字段名称。
如果有一个自动递增字段,则按该字段降序排列。
SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC];
使用DESC
ORDER BY
.
SELECT * FROM <TABLE> ORDER BY <COLUMN> DESC
更改ORDER BY
声明
ORDER BY col
或ORDER BY col ASC
到ORDER BY col DESC
ORDER BY col DESC
到ORDER BY col ASC
MySQL SELECT 的一般查询语法:
SELECT field1, field2,...fieldN FROM table_name
[WHERE Clause][GROUP BY]
[ORDER BY][LIMIT]
示例查询 1:
SELECT id,first_name,last_name,email FROM users ORDER BY first_name desc // In this case you can only fetch data by = id,first_name,last_name,email
示例查询 2:
SELECT * FROM users ORDER BY first_name desc // In this case all fields will be selected
注意: ORDER BY ASC = 数据将按升序排序 & ORDER BY DESC = 数据将按降序排序
编写以下查询以检索数据:
SELECT * FROM `table_name` order by id desc
它将按降序从表中检索数据。