Mengurutkan Berdasarkan Waktu dalam Sehari - MySQL Dasar #42

Untuk dapat mengurutkan waktu dalam sehari Anda perlu mengekstrak jam, menit, dan detik dari kolom yang memuat nilai-nilai waktu dan menggunakannya untuk pengurutan. Contohnya seperti berikut :

Daftar isi : Pengurutan Hasil Query 

MariaDB [root93]> SELECT * FROM mail ORDER by HOUR(t), MINUTE(t), SECOND(t);
+---------------------+---------+---------+---------+---------+--------+
| t                   | srcuser | srchost | dstuser | dsthost | ukuran |
+---------------------+---------+---------+---------+---------+--------+
| 2021-10-13 10:36:44 | tricia  | mars    | gene    | venus   |  19263 |
| 2021-10-13 10:37:20 | phil    | mars    | phil    | saturn  |   1048 |
| 2021-10-13 10:38:06 | barb    | saturn  | tricia  | venus   |    271 |
| 2021-10-13 10:38:54 | phil    | mars    | tricia  | saturn  |   5781 |
| 2021-10-13 10:39:37 | gene    | venus   | barb    | mars    |   2291 |
| 2021-10-13 10:40:07 | barb    | venus   | barb    | venus   |  98161 |
| 2021-10-13 10:40:45 | tricia  | saturn  | phil    | venus   | 239238 |
| 2021-10-13 10:41:27 | gene    | mars    | gene    | saturn  |   3623 |
| 2021-10-13 10:41:27 | gene    | saturn  | gene    | mars    |  32682 |
| 2021-10-13 10:42:32 | phil    | venus   | barb    | venus   |   7823 |
| 2021-10-13 10:42:32 | gene    | venus   | barb    | mars    |   3872 |
| 2021-10-13 10:43:39 | gene    | saturn  | gene    | venus   |  22332 |
| 2021-10-13 12:32:41 | barb    | saturn  | tricia  | mars    |  58274 |
+---------------------+---------+---------+---------+---------+--------+
13 rows in set (0.09 sec)

Fungsi TIME_TO_SEC juga dapat digunakan untuk membuang bagian tanggal dan menghasilkan bagian waktu sebagi jumlah detik.

MariaDB [root93]> SELECT * FROM mail ORDER by TIME_TO_SEC(t);
+---------------------+---------+---------+---------+---------+--------+
| t                   | srcuser | srchost | dstuser | dsthost | ukuran |
+---------------------+---------+---------+---------+---------+--------+
| 2021-10-13 10:36:44 | tricia  | mars    | gene    | venus   |  19263 |
| 2021-10-13 10:37:20 | phil    | mars    | phil    | saturn  |   1048 |
| 2021-10-13 10:38:06 | barb    | saturn  | tricia  | venus   |    271 |
| 2021-10-13 10:38:54 | phil    | mars    | tricia  | saturn  |   5781 |
| 2021-10-13 10:39:37 | gene    | venus   | barb    | mars    |   2291 |
| 2021-10-13 10:40:07 | barb    | venus   | barb    | venus   |  98161 |
| 2021-10-13 10:40:45 | tricia  | saturn  | phil    | venus   | 239238 |
| 2021-10-13 10:41:27 | gene    | mars    | gene    | saturn  |   3623 |
| 2021-10-13 10:41:27 | gene    | saturn  | gene    | mars    |  32682 |
| 2021-10-13 10:42:32 | phil    | venus   | barb    | venus   |   7823 |
| 2021-10-13 10:42:32 | gene    | venus   | barb    | mars    |   3872 |
| 2021-10-13 10:43:39 | gene    | saturn  | gene    | venus   |  22332 |
| 2021-10-13 12:32:41 | barb    | saturn  | tricia  | mars    |  58274 |
+---------------------+---------+---------+---------+---------+--------+
13 rows in set (0.00 sec)

Baca selanjutnya : Mengurutkan Berdasarkan Panjang Substring - MySQL Dasar #43

0 Response to "Mengurutkan Berdasarkan Waktu dalam Sehari - MySQL Dasar #42"

Post a Comment

Komentar yang Anda kirim akan terlebih dahulu di moderasi oleh Admin