Created
December 17, 2013 14:30
-
-
Save hidayat365/8005727 to your computer and use it in GitHub Desktop.
MySQL, menggunakan query untuk menentukan apakah masa garansi sudah habis atau belum berdasarkan tanggal hari ini dan tanggal akhir masa garansi.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mysql> use test ; | |
Database changed | |
mysql> -- -------------------------------------- | |
mysql> -- create table contoh | |
mysql> -- -------------------------------------- | |
mysql> create table garansi ( | |
-> id int auto_increment primary key, | |
-> awal_garansi date null, | |
-> akhir_garansi date null | |
-> ) ; | |
Query OK, 0 rows affected (0.11 sec) | |
mysql> -- -------------------------------------- | |
mysql> -- insert sample data | |
mysql> -- -------------------------------------- | |
mysql> insert into | |
-> garansi (awal_garansi, akhir_garansi) | |
-> values ('2010-07-17', '2013-07-16') | |
-> , ('2011-09-24', '2014-09-23') | |
-> , ('2010-10-14', '2013-10-23') | |
-> , ('2012-01-14', '2015-01-23') | |
-> ; | |
Query OK, 4 rows affected (0.05 sec) | |
Records: 4 Duplicates: 0 Warnings: 0 | |
mysql> -- -------------------------------------- | |
mysql> -- lihat isi sample data | |
mysql> -- -------------------------------------- | |
mysql> select * from garansi ; | |
+----+--------------+---------------+ | |
| id | awal_garansi | akhir_garansi | | |
+----+--------------+---------------+ | |
| 1 | 2010-07-17 | 2013-07-16 | | |
| 2 | 2011-09-24 | 2014-09-23 | | |
| 3 | 2010-10-14 | 2013-10-23 | | |
| 4 | 2012-01-14 | 2015-01-23 | | |
+----+--------------+---------------+ | |
4 rows in set (0.00 sec) | |
mysql> -- -------------------------------------- | |
mysql> -- lakukan query | |
mysql> -- - tampilkan tanggal hari ini | |
mysql> -- - hitung selisih tanggal hari ini dengan akhir garansi | |
mysql> -- - dengan logika percabangan, | |
mysql> -- => jika selisih positif artinya garansi sudah habis, | |
mysql> -- => sebaliknya jika minus, maka garansi masih berlaku | |
mysql> -- -------------------------------------- | |
mysql> select garansi.* | |
-> , current_date tanggal | |
-> , datediff(current_date,akhir_garansi) selisih | |
-> , case | |
-> when datediff(current_date,akhir_garansi)>0 | |
-> then 'habis' | |
-> else 'aktif' end status | |
-> from garansi ; | |
+----+--------------+---------------+------------+---------+--------+ | |
| id | awal_garansi | akhir_garansi | tanggal | selisih | status | | |
+----+--------------+---------------+------------+---------+--------+ | |
| 1 | 2010-07-17 | 2013-07-16 | 2013-12-17 | 154 | habis | | |
| 2 | 2011-09-24 | 2014-09-23 | 2013-12-17 | -280 | aktif | | |
| 3 | 2010-10-14 | 2013-10-23 | 2013-12-17 | 55 | habis | | |
| 4 | 2012-01-14 | 2015-01-23 | 2013-12-17 | -402 | aktif | | |
+----+--------------+---------------+------------+---------+--------+ | |
4 rows in set (0.00 sec) | |
mysql> -- -------------------------------------- | |
mysql> -- YAAAYYY !!! BERHASIL !!! | |
mysql> -- Silakan gunakan query terakhir dalam program php Anda | |
mysql> -- -------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment