Thursday, 13 September 2018

MySQL GROUP BY

Perhatikan Query berikut :


mysql> select id,nomor, kode   from jawaban group by nomor, kode  having count(*) > 1 ;
+--------+------------+------+
| id     | nomor      | kode |
+--------+------------+------+
| 123673 | 2020800548 | 333  |
|  82207 | 2022800062 | 435  |
|  89616 | 2024200252 | 332  |
| 139145 | 2024801290 | 334  |
|  69294 | 2063702348 | 013  |
|  69100 | 2063703029 | 011  |
|  70004 | 2083700083 |      |
+--------+------------+------+
7 rows in set (0.17 sec)
Lalu :
mysql> select MIN(id),nomor, kode   from jawaban group by nomor, kode  having count(*) > 1 ;
+---------+------------+------+
| MIN(id) | nomor      | kode |
+---------+------------+------+
|  123673 | 2020800548 | 333  |
|   82207 | 2022800062 | 435  |
|   89616 | 2024200252 | 332  |
|  139145 | 2024801290 | 334  |
|   69294 | 2063702348 | 013  |
|   69100 | 2063703029 | 011  |
|   70004 | 2083700083 |      |
+---------+------------+------+
7 rows in set (0.17 sec)
Kemudian :
mysql> select MAX(id),nomor, kode   from jawaban group by nomor, kode  having count(*) > 1 ;
+---------+------------+------+
| MAX(id) | nomor      | kode |
+---------+------------+------+
|  124862 | 2020800548 | 333  |
|   82346 | 2022800062 | 435  |
|   90074 | 2024200252 | 332  |
|  140915 | 2024801290 | 334  |
|   77558 | 2063702348 | 013  |
|   77380 | 2063703029 | 011  |
|   78377 | 2083700083 |      |
+---------+------------+------+
7 rows in set (0.19 sec)

Kita bisa menentukan, dalam member group yg duplikat, data mana yang ingin kita ambil secara default.

No comments:

Post a Comment