[できるPRO MySQL できるPROシリーズ Kindle版] まとめ

Logo

できるPRO MySQL できるPROシリーズ Kindle版の内容をまとめる。

View the Project on GitHub nekonisi/MySQL

09_データのソート

概要

ORDER BY

概要

文法

ORDER BY 列名 [並び順] [, …]

並び順について

並び順 意味
ASC(ascent) 昇順(小さいものから)
DESC(descent) 降順(大きいものから)

サンプル

昇順に並べて絞り込む

SELECT * FROM city 
ORDER BY Population 
LIMIT 5;
MariaDB [world]> SELECT * FROM city ORDER BY Population LIMIT 5;
+------+--------------------+-------------+-------------+------------+
| ID   | Name               | CountryCode | District    | Population |
+------+--------------------+-------------+-------------+------------+
| 2912 | Adamstown          | PCN         | ?           |         42 |
| 2317 | West Island        | CCK         | West Island |        167 |
| 3333 | Fakaofo            | TKL         | Fakaofo     |        300 |
| 3538 | Citt? del Vaticano | VAT         | ?           |        455 |
| 2316 | Bantam             | CCK         | Home Island |        503 |
+------+--------------------+-------------+-------------+------------+
5 rows in set (0.01 sec)

降順に並べて絞り込む

SELECT * FROM city 
ORDER BY Population DESC 
LIMIT 5;
MariaDB [world]> SELECT * FROM city ORDER BY Population DESC LIMIT 5;
+------+-----------------+-------------+--------------+------------+
| ID   | Name            | CountryCode | District     | Population |
+------+-----------------+-------------+--------------+------------+
| 1024 | Mumbai (Bombay) | IND         | Maharashtra  |   10500000 |
| 2331 | Seoul           | KOR         | Seoul        |    9981619 |
|  206 | S?o Paulo       | BRA         | S?o Paulo    |    9968485 |
| 1890 | Shanghai        | CHN         | Shanghai     |    9696300 |
|  939 | Jakarta         | IDN         | Jakarta Raya |    9604900 |
+------+-----------------+-------------+--------------+------------+
5 rows in set (0.01 sec)