Perintah/Script SQL NOT EXISTS() dan EXISTS() di MySQL Server
https://carakuvb6.blogspot.com/2017/12/perintahscript-sql-not-exists-dan.html
Untuk perintah/script fungsi NOT EXISTS() dan EXISTS() pada MySQL Database menurut pengalaman admin hampir sama dengan fungsi IN() dan NOT IN(). Fungsi ini untuk menampikan data dari 2 buah tabel dimana salah satu tabel dengan data yang sama atau tidak sama pada tabel kedua menggunakan acuan primarykey. Langsung saja kita implementasikan di MySQL Database Server Command Line.
Kali ini admin menggunakan dua tabel barang dan tabel stok, kita tampilkan terlebih dahulu untuk tabel barang
mysql> select * from barang limit 15;
+---------------+--------------------------+--------+
| IDBARANG | NMBARANG | JUMBRG |
+---------------+--------------------------+--------+
| 0001 | SAMPOERNA MILD 16 BATANG | 30 |
| 0001 | SAMPOERNA MILD 16 BATANG | 100 |
| 0001 | SAMPOERNA MILD 16 BATANG | 100 |
| 1111234555 | U MILD 16 | 10 |
| 8998989300087 | GG MILD | 10 |
| 8999909096004 | SAMPOERNA MILD 16 | 100 |
| 76164217 | MALBORO RED 20 | 10 |
| 8999909000483 | AVOLUTION MENTOL 20 | 10 |
| 95508788 | MALBORO BLACK MENTOL20 | 10 |
| 8995078803078 | U MILD 16 | 15 |
| 8999909982000 | A MILD 12 | 10 |
| 8999909010567 | DJI SAM SOE 16 | 10 |
| 8999909001909 | SAM SOE MAGNUM | 10 |
| 8999909000346 | U BOLD | 20 |
| 8999909000377 | AVOLUTION RED 20 | 10 |
+---------------+--------------------------+--------+
15 rows in set (0.00 sec)
Kemudian untuk tabel stok
mysql> select * from barang limit 15;
+---------------+--------------------------+--------+
| IDBARANG | NMBARANG | JUMBRG |
+---------------+--------------------------+--------+
| 0001 | SAMPOERNA MILD 16 BATANG | 30 |
| 0001 | SAMPOERNA MILD 16 BATANG | 100 |
| 0001 | SAMPOERNA MILD 16 BATANG | 100 |
| 1111234555 | U MILD 16 | 10 |
| 8998989300087 | GG MILD | 10 |
| 8999909096004 | SAMPOERNA MILD 16 | 100 |
| 76164217 | MALBORO RED 20 | 10 |
| 8999909000483 | AVOLUTION MENTOL 20 | 10 |
| 95508788 | MALBORO BLACK MENTOL20 | 10 |
| 8995078803078 | U MILD 16 | 15 |
| 8999909982000 | A MILD 12 | 10 |
| 8999909010567 | DJI SAM SOE 16 | 10 |
| 8999909001909 | SAM SOE MAGNUM | 10 |
| 8999909000346 | U BOLD | 20 |
| 8999909000377 | AVOLUTION RED 20 | 10 |
+---------------+--------------------------+--------+
15 rows in set (0.00 sec)
Kemudian untuk tabel stok
mysql> select * from stok limit 15;
+---------------+--------------------------+--------+
| IDBARANG | NMBARANG | JUMBRG |
+---------------+--------------------------+--------+
| 0001 | SAMPOERNA MILD 16 BATANG | 228 |
| 1111234555 | U MILD 16 | 5 |
| 8998989300087 | GG MILD | 17 |
| 8999909096004 | SAMPOERNA MILD 16 | 62 |
| 76164217 | MALBORO RED 20 | 26 |
| P | PERMEN SEMUA JENIS ISI 5 | 27 |
| 95508788 | MALBORO BLACK MENTOL20 | 3 |
| 8995078803078 | U MILD 16 | 21 |
| 8999909982000 | A MILD 12 | 67 |
| 8999909010567 | DJI SAM SOE 16 | 38 |
| 8999909001909 | SAM SOE MAGNUM | 16 |
| 8999909000346 | U BOLD | 30 |
| 8999909000377 | AVOLUTION RED 20 | 94 |
| 0050 | KARTON SEMUA JENIS | 50 |
| 8999909028234 | SAMSOE 12 | 8 |
+---------------+--------------------------+--------+
15 rows in set (0.00 sec)
mysql> select * from barang where
-> EXISTS (select idbarang from stok where barang.idbarang=stok.idbarang)
-> limit 15;
+---------------+--------------------------+--------+
| IDBARANG | NMBARANG | JUMBRG |
+---------------+--------------------------+--------+
| 0001 | SAMPOERNA MILD 16 BATANG | 30 |
| 0001 | SAMPOERNA MILD 16 BATANG | 100 |
| 0001 | SAMPOERNA MILD 16 BATANG | 100 |
| 1111234555 | U MILD 16 | 10 |
| 8998989300087 | GG MILD | 10 |
| 8999909096004 | SAMPOERNA MILD 16 | 100 |
| 76164217 | MALBORO RED 20 | 10 |
| 8999909000483 | AVOLUTION MENTOL 20 | 10 |
| 95508788 | MALBORO BLACK MENTOL20 | 10 |
| 8995078803078 | U MILD 16 | 15 |
| 8999909982000 | A MILD 12 | 10 |
| 8999909010567 | DJI SAM SOE 16 | 10 |
| 8999909001909 | SAM SOE MAGNUM | 10 |
| 8999909000346 | U BOLD | 20 |
| 8999909000377 | AVOLUTION RED 20 | 10 |
+---------------+--------------------------+--------+
15 rows in set (0.00 sec)
Data yang tampil diatas merupakan tabel data barang dengan primarykey yang sama pada tabel stok, bagaimana jika kita akan menampilkan data barang dengan primary key yang tidak sama pada tabel stok
mysql> select * from barang where
-> NOT EXISTS (select idbarang from stok where barang.idbarang=stok.idbarang)
-> limit 15;
+---------------+----------------------------------+--------+
| IDBARANG | NMBARANG | JUMBRG |
+---------------+----------------------------------+--------+
| AART | AL-AZKA ROTI TAWAR | 2 |
| 8992759184006 | JOLLY TISSU K | 150 |
| ASJ | ALAMIN SUPER JUMBO | 6 |
| 8992756333377 | BIR ANKER STOUT620ML | 20 |
| 8998667400924 | MINYAK TELON PLUS KONICARE 30ML | 2 |
| 8992775311998 | CHOCOLATOS GRANDE ISI 20PCS | 3 |
| 8992745320135 | STELLA GANTUNG SENSATION NATURAL | 6 |
| 8999999049423 | REXONA MEN SPORT DEFENCE ROLL ON | 3 |
| 8886467100260 | PRINGLES ORI 42G | 3 |
| 8886467100017 | PRINGLES ORI 110G | 3 |
| 8992761164560 | NUTRIBOOST APEL | 12 |
| 8992759184006 | JOLLY TISSU K | 0 |
| 0049 | KARET GELANG PAK TANI | 0 |
| 0051 | KARTU GAPLE KALAJENGKING | 0 |
| 0052 | KCG BAWANG LPG SLTN | 0 |
+---------------+----------------------------------+--------+
15 rows in set (0.01 sec)
Semoga Bermanfaat
sama2 gan, trima kasih juga atas kunjungannya
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteTerimakasih banyak gan sangat2 membantu kebetulan saya juga lagi mengerjakan project yang mengharuskan penggunaan query seperti query di atas. Thanks
ReplyDelete