取得順序

ORDER BY句は、クエリ結果の行を特定の列の値に基づいて並べ替えるために使用されます。

ASC : 小さい値から昇順
DESC : 大きい値から降順

ORDER BY
記述

ORDER BY カラム名 ソート指定

【通常】
mysql> SELECT * FROM informations;
+----+------------+---------------------+---------------------+
| id | name       | created_at          | updated_at          |
+----+------------+---------------------+---------------------+
|  1 | test info1 | 2024-04-01 00:01:00 | 2024-04-01 00:01:00 |
|  2 | test info2 | 2024-04-01 00:02:00 | 2024-04-01 00:02:00 |
|  3 | test info3 | 2024-04-01 00:03:00 | 2024-04-01 00:03:00 |
|  4 | test info4 | 2024-04-01 00:04:00 | 2024-04-01 00:04:00 |
+----+------------+---------------------+---------------------+
4 rows in set (0.00 sec)

【降順】
mysql> SELECT * FROM informations ORDER BY id DESC;
+----+------------+---------------------+---------------------+
| id | name       | created_at          | updated_at          |
+----+------------+---------------------+---------------------+
|  4 | test info4 | 2024-04-01 00:04:00 | 2024-04-01 00:04:00 |
|  3 | test info3 | 2024-04-01 00:03:00 | 2024-04-01 00:03:00 |
|  2 | test info2 | 2024-04-01 00:02:00 | 2024-04-01 00:02:00 |
|  1 | test info1 | 2024-04-01 00:01:00 | 2024-04-01 00:01:00 |
+----+------------+---------------------+---------------------+
4 rows in set (0.01 sec)