Mengurutkan Berdasarkan Tanggal - MySQL Dasar #39

Pengurutan berdasarkan tipe kolom tanggal atau waktu akan mengabaikan bagian - bagian nilai yang tak relevan

Daftar isi : Pengurutan Hasil Query 

MySQL mengetahui bagaimana mengurutkan tipe - tipe kolom temporal. Dimisalkan bahwa Anda memiliki sebuah tabel, nil_temporal berikut :


MariaDB [root93]> CREATE TABLE nil_temporal(
    -> d DATE,
    -> dt DATETIME,
    -> t TIME,
    -> ts TIMESTAMP);
Query OK, 0 rows affected (0.78 sec)

MariaDB [root93]> INSERT INTO nil_temporal (d, dt, t, ts)
    -> VALUES ('1970-01-01','1884-01-01 12:00:00','13:00:00','1980-01-01 02:00:00');
Query OK, 1 row affected (0.11 sec)
MariaDB [root93]> INSERT INTO nil_temporal (d, dt, t, ts)
    -> VALUES ('1999-01-01','1860-01-01 12:00:00','19:00:00','2021-01-01 03:00:00');
Query OK, 1 row affected (0.05 sec)

MariaDB [root93]> INSERT INTO nil_temporal (d, dt, t, ts)
    -> VALUES('1981-01-01','1871-01-01 12:00:00','03:00:00','1975-01-01 04:00:00');
Query OK, 1 row affected (0.05 sec)

MariaDB [root93]> INSERT INTO nil_temporal (d, dt, t, ts)
    -> VALUES ('1964-01-01','1899-01-01 12:00:00','01:00:00','1985-01-01 05:00:00');
Query OK, 1 row affected (0.05 sec)

MariaDB [root93]> SELECT * FROM nil_temporal;
+------------+---------------------+----------+---------------------+
| d          | dt                  | t        | ts                  |
+------------+---------------------+----------+---------------------+
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 1980-01-01 02:00:00 |
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 2021-01-01 03:00:00 |
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 1975-01-01 04:00:00 |
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 1985-01-01 05:00:00 |
+------------+---------------------+----------+---------------------+
4 rows in set (0.00 sec)

Penggunaan klausa ORDER BY pada sembarang kolom akan mengurutkan nilai- nilai pada kolom tersebut :

MariaDB [root93]> SELECT * FROM nil_temporal ORDER BY d;
+------------+---------------------+----------+---------------------+
| d          | dt                  | t        | ts                  |
+------------+---------------------+----------+---------------------+
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 1985-01-01 05:00:00 |
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 1980-01-01 02:00:00 |
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 1975-01-01 04:00:00 |
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 2021-01-01 03:00:00 |
+------------+---------------------+----------+---------------------+
4 rows in set (0.00 sec)

MariaDB [root93]> SELECT * FROM nil_temporal ORDER BY dt;
+------------+---------------------+----------+---------------------+
| d          | dt                  | t        | ts                  |
+------------+---------------------+----------+---------------------+
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 2021-01-01 03:00:00 |
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 1975-01-01 04:00:00 |
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 1980-01-01 02:00:00 |
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 1985-01-01 05:00:00 |
+------------+---------------------+----------+---------------------+
4 rows in set (0.00 sec)

MariaDB [root93]> SELECT * FROM nil_temporal ORDER BY t;
+------------+---------------------+----------+---------------------+
| d          | dt                  | t        | ts                  |
+------------+---------------------+----------+---------------------+
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 1985-01-01 05:00:00 |
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 1975-01-01 04:00:00 |
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 1980-01-01 02:00:00 |
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 2021-01-01 03:00:00 |
+------------+---------------------+----------+---------------------+
4 rows in set (0.00 sec)

MariaDB [root93]> SELECT * FROM nil_temporal ORDER BY ts;
+------------+---------------------+----------+---------------------+
| d          | dt                  | t        | ts                  |
+------------+---------------------+----------+---------------------+
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 1975-01-01 04:00:00 |
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 1980-01-01 02:00:00 |
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 1985-01-01 05:00:00 |
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 2021-01-01 03:00:00 |
+------------+---------------------+----------+---------------------+
4 rows in set (0.00 sec)

Baca Selanjutnya : Mengurutkan Berdasarkan Hari Kalender - MySQL Dasar #40

0 Response to "Mengurutkan Berdasarkan Tanggal - MySQL Dasar #39"

Post a Comment

Komentar yang Anda kirim akan terlebih dahulu di moderasi oleh Admin