MySQL : Sorting Baris

Oleh : Reza Ervani

بسم الله الرحمن الرحيم

Pada kesempatan ini kita lihat bagaimana cara mensorting baris :

Anggap kita mempunyai tabel dengan data seperti berikut :


mysql> SELECT * FROM datadantanggal;
+----+----------+------------+-------+
| id | category | tanggal    | topic |
+----+----------+------------+-------+
|  1 |        1 | 2014-06-01 |     1 |
|  2 |        2 | 2014-06-02 |     1 |
|  3 |        1 | 2014-06-03 |     1 |
|  4 |        1 | 2014-05-02 |     2 |
|  5 |        3 | 2014-05-02 |     2 |
|  6 |        2 | 2014-05-07 |     1 |
|  7 |        2 | 2014-05-06 |     2 |
|  8 |        1 | 2014-05-13 |     3 |
|  9 |        2 | 2014-05-18 |     1 |
| 10 |        3 | 2014-05-05 |     2 |
| 11 |        2 | 2014-05-07 |     1 |
+----+----------+------------+-------+
11 rows in set (0.00 sec)

Kita ingin mengurutkan data tersebut :

Mulai dari tanggal terbaru, maka perintahnya di MySQL

Maka perintahnya :

SELECT * FROM datadantanggal ORDER BY tanggal DESC;

Dimana kita mengurutkan berdasarkan kolom tanggal secara menurun (Descending)

Tampilannya akan tampak seperti berikut :


mysql> SELECT * FROM datadantanggal ORDER BY tanggal DESC;
+----+----------+------------+-------+
| id | category | tanggal    | topic |
+----+----------+------------+-------+
|  3 |        1 | 2014-06-03 |     1 |
|  2 |        2 | 2014-06-02 |     1 |
|  1 |        1 | 2014-06-01 |     1 |
|  9 |        2 | 2014-05-18 |     1 |
|  8 |        1 | 2014-05-13 |     3 |
|  6 |        2 | 2014-05-07 |     1 |
| 11 |        2 | 2014-05-07 |     1 |
|  7 |        2 | 2014-05-06 |     2 |
| 10 |        3 | 2014-05-05 |     2 |
|  5 |        3 | 2014-05-02 |     2 |
|  4 |        1 | 2014-05-02 |     2 |
+----+----------+------------+-------+
11 rows in set (0.00 sec)

Berdasarkan urutan topik dan tanggal terbaru.

Maka perintahnya : SELECT * FROM datadantanggal ORDER BY topic ASC, tanggal DESC;

Dimana kita mengurutkan berdasarkan topik secara menaik (ascending) dan tanggal secara descending.


mysql> SELECT * FROM datadantanggal ORDER BY topic ASC, tanggal DESC;
+----+----------+------------+-------+
| id | category | tanggal    | topic |
+----+----------+------------+-------+
|  3 |        1 | 2014-06-03 |     1 |
|  2 |        2 | 2014-06-02 |     1 |
|  1 |        1 | 2014-06-01 |     1 |
|  9 |        2 | 2014-05-18 |     1 |
|  6 |        2 | 2014-05-07 |     1 |
| 11 |        2 | 2014-05-07 |     1 |
|  7 |        2 | 2014-05-06 |     2 |
| 10 |        3 | 2014-05-05 |     2 |
|  5 |        3 | 2014-05-02 |     2 |
|  4 |        1 | 2014-05-02 |     2 |
|  8 |        1 | 2014-05-13 |     3 |
+----+----------+------------+-------+
11 rows in set (0.00 sec)

Berdasarkan tanggal terbaru di masing-masing topik :

Perintahnya : SELECT * FROM (SELECT * FROM datadantanggal ORDER BY tanggal DESC) AS awal GROUP BY topic;


mysql> SELECT * FROM (SELECT * FROM datadantanggal ORDER BY tanggal DESC) AS awal GROUP BY topic;
+----+----------+------------+-------+
| id | category | tanggal    | topic |
+----+----------+------------+-------+
|  3 |        1 | 2014-06-03 |     1 |
|  7 |        2 | 2014-05-06 |     2 |
|  8 |        1 | 2014-05-13 |     3 |
+----+----------+------------+-------+
3 rows in set (0.04 sec)

Perhatikan bagian AS awal. Ini disebut Alias. Setiap tabel yang dikeluarkan hasilnya (derived) mestilah diberikan alias terlebih dahulu (dalam hal ini kita mengaliaskannya dengan nama “awal”) jika tidak maka akan muncul pesan kesalahan seperti berikut :

ERROR 1248 (42000): Every derived table must have its own alias

Selamat mencoba

About Reza Ervani 387 Articles
Adalah pendiri programming.rezaervani.com -

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.