Ternyata kita perlu menambahkan sebuah kolom field lagi, yaitu kolom gaji, pada tabel karyawan. Kolom Gajimerupakan kolom numerik yang menampung data gaji pokok karyawan per bulannya. Jadi, yang kita perlukan adalah jenis dataINTeger dengan lebar data 12 digit. Penerapannya sebagai berikut dengan menggunakan perintah ALTER.
mysql>
alter table karyawan
-> ADD gaji INT(12) NOT NULL default 0
-> ;
Query OK, 6 rows affected (0.25 sec)
Records: 6 Duplicates: 0 Warnings: 0
-> ADD gaji INT(12) NOT NULL default 0
-> ;
Query OK, 6 rows affected (0.25 sec)
Records: 6 Duplicates: 0 Warnings: 0
Kita periksa struktur tabelnya dulu:
mysql>
describe karyawan ;
+--------+-----------+----+----+-------+--------------+
|Field |Type |Null| Key|Default|Extra |
+--------+-----------+----+----+-------+--------------+
|noid |int(10) |NO | PRI|NULL |auto_increment|
|nama |varchar(50)|NO | | | |
|jenkel |char(1) |YES | |NULL | |
|kota |varchar(25)|NO | | | |
|kodepos |char(5) |NO | | | |
|tgllahir|date |YES | |NULL | |
|gaji |int(12) |NO | |0 | |
+--------+-----------+----+----+-------+--------------+
7 rows in set (0.01 sec)
+--------+-----------+----+----+-------+--------------+
|Field |Type |Null| Key|Default|Extra |
+--------+-----------+----+----+-------+--------------+
|noid |int(10) |NO | PRI|NULL |auto_increment|
|nama |varchar(50)|NO | | | |
|jenkel |char(1) |YES | |NULL | |
|kota |varchar(25)|NO | | | |
|kodepos |char(5) |NO | | | |
|tgllahir|date |YES | |NULL | |
|gaji |int(12) |NO | |0 | |
+--------+-----------+----+----+-------+--------------+
7 rows in set (0.01 sec)
Ya, kolom gaji sudah ditambahkan ke dalam tabel karyawan. Sekarang kita akan menambahkan data gaji kepada tiap-tiap karyawan yang ada. Untuk memudahkan, kita tampilkan dulu semua data yang ada di tabel karyawan:
mysql>
select * from karyawan ;
+----+--------------+------+---------+--------+-----------+----+
|noid| nama |jenkel| kota | kodepos| tgllahir |gaji|
+----+--------------+------+---------+--------+-----------+----+
|1 | Ahmad Sobari |L | Bandung | 41011 | 1977-10-02| 0 |
|2 | Sundariwati |P | Bandung | 40123 | 1978-11-12| 0 |
|3 | Ryan Cakep |L | Jakarta | 12111 | 1981-03-21| 0 |
|4 | Zukarman |L | Bekasi | 17211 | 1978-08-10| 0 |
|5 | Yuliawati |P | Bogor | 00000 | 1982-06-09| 0 |
|6 | Mawar |P | Bogor | 12345 | 1985-07-07| 0 |
+----+--------------+------+---------+--------+-----------+----+
6 rows in set (0.00 sec)
+----+--------------+------+---------+--------+-----------+----+
|noid| nama |jenkel| kota | kodepos| tgllahir |gaji|
+----+--------------+------+---------+--------+-----------+----+
|1 | Ahmad Sobari |L | Bandung | 41011 | 1977-10-02| 0 |
|2 | Sundariwati |P | Bandung | 40123 | 1978-11-12| 0 |
|3 | Ryan Cakep |L | Jakarta | 12111 | 1981-03-21| 0 |
|4 | Zukarman |L | Bekasi | 17211 | 1978-08-10| 0 |
|5 | Yuliawati |P | Bogor | 00000 | 1982-06-09| 0 |
|6 | Mawar |P | Bogor | 12345 | 1985-07-07| 0 |
+----+--------------+------+---------+--------+-----------+----+
6 rows in set (0.00 sec)
memperbaharui data
pada tabel
Sekarang kita masukkan data gaji masing-masing karyawan dengan menggunakan perintah UPDATE... SET... Kita mulai dari Ahmad Sobari, dengan noid=1:
mysql>
update karyawan
-> set gaji=1000000
-> where noid=1 ;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-> set gaji=1000000
-> where noid=1 ;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Periksa dulu hasilnya:
mysql>
select * from karyawan
-> where noid=1 ;
+----+------------+------+-------+-------+----------+-------+
|noid|nama |jenkel|kota |kodepos|tgllahir |gaji |
+----+------------+------+-------+-------+----------+-------+
| 1 |Ahmad Sobari|L |Bandung|41011 |1977-10-02|1000000|
+----+------------+------+-------+-------+----------+-------+
1 row in set (0.00 sec)
-> where noid=1 ;
+----+------------+------+-------+-------+----------+-------+
|noid|nama |jenkel|kota |kodepos|tgllahir |gaji |
+----+------------+------+-------+-------+----------+-------+
| 1 |Ahmad Sobari|L |Bandung|41011 |1977-10-02|1000000|
+----+------------+------+-------+-------+----------+-------+
1 row in set (0.00 sec)
Kita lanjutkan dengan karyawan lainnya, seperti Sundariwati dengan noid=2, Ryan Cakep dengan noid=3, dan seterusnya.
Sayangnya, perintah ini hanya bisa dilakukan satu per satu. Jadi, Anda harus sabar menjalankan perintah di bawah ini yaa..:
mysql>
update karyawan
-> set gaji=1250000 where noid=2 ;
Query OK, 1 row affected (0.39 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-> set gaji=1250000 where noid=2 ;
Query OK, 1 row affected (0.39 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
update karyawan
-> set gaji=1500000 where noid=3 ;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-> set gaji=1500000 where noid=3 ;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
update karyawan
-> set gaji=1750000 where noid=4 ;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-> set gaji=1750000 where noid=4 ;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
update karyawan
-> set gaji=2000000 where noid=5 ;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-> set gaji=2000000 where noid=5 ;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
update karyawan
-> set gaji=2250000 where noid=6 ;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-> set gaji=2250000 where noid=6 ;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Kita periksa semua hasilnya:
mysql>
select * from karyawan ;
+----+--------------+------+--------+--------+-----------+--------+
|noid| nama |jenkel| kota | kodepos| tgllahir | gaji |
+----+--------------+------+--------+--------+-----------+--------+
|1 | Ahmad Sobari |L | Bandung| 41011 | 1977-10-02| 1000000|
|2 | Sundariwati |P | Bandung| 40123 | 1978-11-12| 1250000|
|3 | Ryan Cakep |L | Jakarta| 12111 | 1981-03-21| 1500000|
|4 | Zukarman |L | Bekasi | 17211 | 1978-08-10| 1750000|
|5 | Yuliawati |P | Bogor | 00000 | 1982-06-09| 2000000|
|6 | Mawar |P | Bogor | 12345 | 1985-07-07| 2250000|
+----+--------------+------+--------+--------+-----------+--------+
6 rows in set (0.00 sec)
+----+--------------+------+--------+--------+-----------+--------+
|noid| nama |jenkel| kota | kodepos| tgllahir | gaji |
+----+--------------+------+--------+--------+-----------+--------+
|1 | Ahmad Sobari |L | Bandung| 41011 | 1977-10-02| 1000000|
|2 | Sundariwati |P | Bandung| 40123 | 1978-11-12| 1250000|
|3 | Ryan Cakep |L | Jakarta| 12111 | 1981-03-21| 1500000|
|4 | Zukarman |L | Bekasi | 17211 | 1978-08-10| 1750000|
|5 | Yuliawati |P | Bogor | 00000 | 1982-06-09| 2000000|
|6 | Mawar |P | Bogor | 12345 | 1985-07-07| 2250000|
+----+--------------+------+--------+--------+-----------+--------+
6 rows in set (0.00 sec)
Cukup mudah kan?
Itulah dasar-dasar menggunakan perintah MySQL. Sekarang kita membutuhkan lebih
banyak data untuk latihan kita. Minimal sekitar 30-an tambahan data lagi. Tapi
apakah ada cara lain yang lebih mudah dibanding harus mengetikkan datanya satu
per satu? Kan kalo kita ketik satu per satu, akan ada resiko kesalahan ketik
karena faktor kelelahan, dan sebagainya. Untungnya untuk pemasukan data masal
kita bisa menggunakan cara yang lebih mudah.
memasukkan data secara
masal
Untuk pemasukan data secara masal, kita menggunakan data-data yang
telah ditulis dalam sebuah file teks biasa. File ini kita namakan tambahdata.txt, dan untuk contoh ini kita simpan di dalam
folder C:\Data. Anda dapat mengunduh (download) file tambahdata.txt dari tautan (link) dibawah ini:
{phocadownload view=file|id=1|target=s}
Perintah yang kita gunakan untuk memasukkan data secara masal
adalah "load data local infile".
mysql>
load data local infile 'C:\data\tambahdata.txt'
-> into table karyawan
-> fields terminated by ','
-> lines terminated by 'n'
-> ;
Query OK, 36 rows affected, 36 warnings (0.47 sec)
Records: 36 Deleted: 0 Skipped: 0 Warnings: 0
-> into table karyawan
-> fields terminated by ','
-> lines terminated by 'n'
-> ;
Query OK, 36 rows affected, 36 warnings (0.47 sec)
Records: 36 Deleted: 0 Skipped: 0 Warnings: 0
Sekarang kita lihat hasilnya di tabel karyawan:
mysql>
select * from karyawan ;
+----+--------------+------+-----------+-------+-----------+-------+
|noid| nama |jenkel|kota |kodepos|tgllahir |gaji |
+----+--------------+------+-----------+-------+-----------+-------+
| 1 | Ahmad Sobari |L |Bandung |41011 |1977-10-02 |1000000|
| 2 | Sundariwati |P |Bandung |40123 |1978-11-12 |1250000|
| 3 | Ryan Cakep |L |Jakarta |12111 |1981-03-21 |1500000|
| 4 | Zukarman |L |Bekasi |17211 |1978-08-10 |1750000|
| 5 | Yuliawati |P |Bogor |00000 |1982-06-09 |2000000|
| 6 | Mawar |P |Bogor |12345 |1985-07-07 |2250000|
| 7 | Sobari |L |Jakarta |41011 |1976-10-02 |1100000|
| 8 | Melia |P |Bandung |40123 |1979-11-12 |1200000|
| 9 | Zanda Cute |L |Jakarta |12111 |1980-03-21 |1300000|
| 10 | Maman |L |Bekasi |17211 |1977-08-10 |1400000|
| 11 | Yenny |P |Bogor |00000 |1985-06-09 |1150000|
| 12 | Rossa |P |Jakarta |12345 |1987-07-07 |1350000|
| 13 | Dadan |L |Bandung |41011 |1975-10-02 |1450000|
| 14 | Wawan |P |Semarang |40123 |1971-11-12 |1600000|
| 15 | The Cute |L |Jakarta |12111 |1977-03-21 |1700000|
| 16 | Marpaung |L |Surabaya |17211 |1988-08-10 |1800000|
| 17 | Yono |P |Bogor |00000 |1989-06-09 |1900000|
| 18 | Dian |P |Jakarta |12345 |1980-07-07 |1650000|
| 19 | Donno |L |Bandung |41011 |1971-10-02 |1850000|
| 20 | Ratu |P |Yogyakarta |40123 |1972-11-12 |1950000|
| 21 | Bambang |L |Jakarta |12111 |1982-03-21 |2100000|
| 22 | Dadang |L |Surabaya |17211 |1977-08-10 |2200000|
| 23 | Yuliawati |P |Bogor |00000 |1974-06-09 |2300000|
| 24 | Miranda |P |Bogor |12345 |1980-07-07 |2400000|
| 25 | Subur |L |Bandung |41011 |1977-10-02 |2150000|
| 26 | Banowati |P |Malang |40123 |1978-11-12 |2350000|
| 27 | Gungun |L |Jakarta |12111 |1981-03-21 |2450000|
| 28 | Gunadi |L |Bekasi |17211 |1978-08-10 |2125000|
| 29 | Yossy |P |Bogor |00000 |1982-06-09 |2225000|
| 30 | Melia |P |Malang |12345 |1981-07-07 |2325000|
| 31 | Anwar |L |Purwakarta |41011 |1972-10-02 |2425000|
| 32 | Susilowati |P |Bandung |40123 |1973-11-12 |1125000|
| 33 | Rahmat |L |Jakarta |12111 |1977-03-21 |1225000|
| 34 | Zamzam |L |Bekasi |17211 |1974-08-10 |1325000|
| 35 | Nenny |P |Medan |00000 |1972-06-09 |1425000|
| 36 | Mardiatun |P |Bogor |12345 |1975-07-07 |1625000|
| 37 | Andika |L |Bandung |41011 |1978-10-02 |1725000|
| 38 | Siti |P |Medan |40123 |1988-11-12 |1825000|
| 39 | Rohimat |L |Jakarta |12111 |1980-03-21 |1925000|
| 40 | Beno |L |Bekasi |17211 |1978-08-10 |1175000|
| 41 | Yanti |P |Jakarta |00000 |1981-06-09 |1275000|
| 42 | Miranti |P |Medan |12345 |1975-07-07 |1375000|
+----+--------------+------+-----------+-------+-----------+-------+
42 rows in set (0.00 sec)
+----+--------------+------+-----------+-------+-----------+-------+
|noid| nama |jenkel|kota |kodepos|tgllahir |gaji |
+----+--------------+------+-----------+-------+-----------+-------+
| 1 | Ahmad Sobari |L |Bandung |41011 |1977-10-02 |1000000|
| 2 | Sundariwati |P |Bandung |40123 |1978-11-12 |1250000|
| 3 | Ryan Cakep |L |Jakarta |12111 |1981-03-21 |1500000|
| 4 | Zukarman |L |Bekasi |17211 |1978-08-10 |1750000|
| 5 | Yuliawati |P |Bogor |00000 |1982-06-09 |2000000|
| 6 | Mawar |P |Bogor |12345 |1985-07-07 |2250000|
| 7 | Sobari |L |Jakarta |41011 |1976-10-02 |1100000|
| 8 | Melia |P |Bandung |40123 |1979-11-12 |1200000|
| 9 | Zanda Cute |L |Jakarta |12111 |1980-03-21 |1300000|
| 10 | Maman |L |Bekasi |17211 |1977-08-10 |1400000|
| 11 | Yenny |P |Bogor |00000 |1985-06-09 |1150000|
| 12 | Rossa |P |Jakarta |12345 |1987-07-07 |1350000|
| 13 | Dadan |L |Bandung |41011 |1975-10-02 |1450000|
| 14 | Wawan |P |Semarang |40123 |1971-11-12 |1600000|
| 15 | The Cute |L |Jakarta |12111 |1977-03-21 |1700000|
| 16 | Marpaung |L |Surabaya |17211 |1988-08-10 |1800000|
| 17 | Yono |P |Bogor |00000 |1989-06-09 |1900000|
| 18 | Dian |P |Jakarta |12345 |1980-07-07 |1650000|
| 19 | Donno |L |Bandung |41011 |1971-10-02 |1850000|
| 20 | Ratu |P |Yogyakarta |40123 |1972-11-12 |1950000|
| 21 | Bambang |L |Jakarta |12111 |1982-03-21 |2100000|
| 22 | Dadang |L |Surabaya |17211 |1977-08-10 |2200000|
| 23 | Yuliawati |P |Bogor |00000 |1974-06-09 |2300000|
| 24 | Miranda |P |Bogor |12345 |1980-07-07 |2400000|
| 25 | Subur |L |Bandung |41011 |1977-10-02 |2150000|
| 26 | Banowati |P |Malang |40123 |1978-11-12 |2350000|
| 27 | Gungun |L |Jakarta |12111 |1981-03-21 |2450000|
| 28 | Gunadi |L |Bekasi |17211 |1978-08-10 |2125000|
| 29 | Yossy |P |Bogor |00000 |1982-06-09 |2225000|
| 30 | Melia |P |Malang |12345 |1981-07-07 |2325000|
| 31 | Anwar |L |Purwakarta |41011 |1972-10-02 |2425000|
| 32 | Susilowati |P |Bandung |40123 |1973-11-12 |1125000|
| 33 | Rahmat |L |Jakarta |12111 |1977-03-21 |1225000|
| 34 | Zamzam |L |Bekasi |17211 |1974-08-10 |1325000|
| 35 | Nenny |P |Medan |00000 |1972-06-09 |1425000|
| 36 | Mardiatun |P |Bogor |12345 |1975-07-07 |1625000|
| 37 | Andika |L |Bandung |41011 |1978-10-02 |1725000|
| 38 | Siti |P |Medan |40123 |1988-11-12 |1825000|
| 39 | Rohimat |L |Jakarta |12111 |1980-03-21 |1925000|
| 40 | Beno |L |Bekasi |17211 |1978-08-10 |1175000|
| 41 | Yanti |P |Jakarta |00000 |1981-06-09 |1275000|
| 42 | Miranti |P |Medan |12345 |1975-07-07 |1375000|
+----+--------------+------+-----------+-------+-----------+-------+
42 rows in set (0.00 sec)
Kita sudah memiliki lebih banyak data. Cukuplah untuk bahan latihan-latihan berikutnya...
operator pembanding
dan operator logika
Sudah saatnya kita melangkah ke permainan data yang lebih mengasyikan lagi dengan menggunakan dua operator, yaitu Operator Pembanding dan Operator Logika. Kedua jenis operator ini akan sering digunakan dalam proses "query" data.
Operator Pembanding
Operator
Pembanding
|
Keterangan
|
Lebih
besar
|
>
|
Lebih
kecil
|
<
|
Lebih
besar atau sama dengan
|
>=
|
Lebih
kecil atau sama dengan
|
<=
|
Sama
dengan
|
=
|
Tidak
sama dengan
|
<>
|
Operator Logika
Operator
Logika
|
Keterangan
|
Dan
|
AND
atau &&
|
Atau
|
OR
atau ||
|
Tidak
sama dengan
|
NOT
atau !
|
Tidak
sama dengan
|
<>
|
Berikut ini adalah penerapan dari kedua operator di atas:
Kita tampilkan data karyawan yang tanggal lahirnya sebelum tanggal 1 Januari 1980, dan tampilan data diurut berdasarkan nama. Cukup hanya kolom nama, jenis kelamin dantanggal lahir saja yang ditampilkan:
mysql>
select nama, jenkel, tgllahir
-> from karyawan
-> where tgllahir < "1980-01-01"
-> order by nama ;
+--------------+--------+------------+
| nama | jenkel | tgllahir |
+--------------+--------+------------+
| Ahmad Sobari | L | 1977-10-02 |
| Andika | L | 1978-10-02 |
| Anwar | L | 1972-10-02 |
| Banowati | P | 1978-11-12 |
| Beno | L | 1978-08-10 |
| Dadan | L | 1975-10-02 |
| Dadang | L | 1977-08-10 |
| Donno | L | 1971-10-02 |
| Gunadi | L | 1978-08-10 |
| Maman | L | 1977-08-10 |
| Mardiatun | P | 1975-07-07 |
| Melia | P | 1979-11-12 |
| Miranti | P | 1975-07-07 |
| Nenny | P | 1972-06-09 |
| Rahmat | L | 1977-03-21 |
| Ratu | P | 1972-11-12 |
| Sobari | L | 1976-10-02 |
| Subur | L | 1977-10-02 |
| Sundariwati | P | 1978-11-12 |
| Susilowati | P | 1973-11-12 |
| The Cute | L | 1977-03-21 |
| Wawan | P | 1971-11-12 |
| Yuliawati | P | 1974-06-09 |
| Zamzam | L | 1974-08-10 |
| Zukarman | L | 1978-08-10 |
+--------------+--------+------------+
25 rows in set (0.00 sec)
-> from karyawan
-> where tgllahir < "1980-01-01"
-> order by nama ;
+--------------+--------+------------+
| nama | jenkel | tgllahir |
+--------------+--------+------------+
| Ahmad Sobari | L | 1977-10-02 |
| Andika | L | 1978-10-02 |
| Anwar | L | 1972-10-02 |
| Banowati | P | 1978-11-12 |
| Beno | L | 1978-08-10 |
| Dadan | L | 1975-10-02 |
| Dadang | L | 1977-08-10 |
| Donno | L | 1971-10-02 |
| Gunadi | L | 1978-08-10 |
| Maman | L | 1977-08-10 |
| Mardiatun | P | 1975-07-07 |
| Melia | P | 1979-11-12 |
| Miranti | P | 1975-07-07 |
| Nenny | P | 1972-06-09 |
| Rahmat | L | 1977-03-21 |
| Ratu | P | 1972-11-12 |
| Sobari | L | 1976-10-02 |
| Subur | L | 1977-10-02 |
| Sundariwati | P | 1978-11-12 |
| Susilowati | P | 1973-11-12 |
| The Cute | L | 1977-03-21 |
| Wawan | P | 1971-11-12 |
| Yuliawati | P | 1974-06-09 |
| Zamzam | L | 1974-08-10 |
| Zukarman | L | 1978-08-10 |
+--------------+--------+------------+
25 rows in set (0.00 sec)
MySQL memiliki "keluwesan" dalam penulisan tanggal
selama formatnya mengikuti aturan "tahun-bulan-tanggal". Misal untuk 12 Nopember 1971 dapat ditulis dengan format: 1971-11-12, atau 1971#11#12, atau 19711112, atau711112.
Kita lihat contohnya dibawah ini dimana 1 Januari 1980, "1980-01-01", ditulis dengan 19800101
mysql>
select nama, jenkel, tgllahir
-> from karyawan
-> where tgllahir < 19800101
-> and jenkel="L"
-> order by nama ;
+--------------+--------+------------+
| nama | jenkel | tgllahir |
+--------------+--------+------------+
| Ahmad Sobari | L | 1977-10-02 |
| Andika | L | 1978-10-02 |
| Anwar | L | 1972-10-02 |
| Beno | L | 1978-08-10 |
| Dadan | L | 1975-10-02 |
| Dadang | L | 1977-08-10 |
| Donno | L | 1971-10-02 |
| Gunadi | L | 1978-08-10 |
| Maman | L | 1977-08-10 |
| Rahmat | L | 1977-03-21 |
| Sobari | L | 1976-10-02 |
| Subur | L | 1977-10-02 |
| The Cute | L | 1977-03-21 |
| Zamzam | L | 1974-08-10 |
| Zukarman | L | 1978-08-10 |
+--------------+--------+------------+
15 rows in set (0.00 sec)
-> from karyawan
-> where tgllahir < 19800101
-> and jenkel="L"
-> order by nama ;
+--------------+--------+------------+
| nama | jenkel | tgllahir |
+--------------+--------+------------+
| Ahmad Sobari | L | 1977-10-02 |
| Andika | L | 1978-10-02 |
| Anwar | L | 1972-10-02 |
| Beno | L | 1978-08-10 |
| Dadan | L | 1975-10-02 |
| Dadang | L | 1977-08-10 |
| Donno | L | 1971-10-02 |
| Gunadi | L | 1978-08-10 |
| Maman | L | 1977-08-10 |
| Rahmat | L | 1977-03-21 |
| Sobari | L | 1976-10-02 |
| Subur | L | 1977-10-02 |
| The Cute | L | 1977-03-21 |
| Zamzam | L | 1974-08-10 |
| Zukarman | L | 1978-08-10 |
+--------------+--------+------------+
15 rows in set (0.00 sec)
Kita lihat contohnya di bawah ini bila tanggal "1980-01-01" ditulis dengan cara 800101.
mysql>
select nama, jenkel, tgllahir
-> from karyawan
-> where tgllahir < 800101
-> and jenkel="L"
-> order by nama;
+--------------+--------+------------+
| nama | jenkel | tgllahir |
+--------------+--------+------------+
| Ahmad Sobari | L | 1977-10-02 |
| Andika | L | 1978-10-02 |
| Anwar | L | 1972-10-02 |
| Beno | L | 1978-08-10 |
| Dadan | L | 1975-10-02 |
| Dadang | L | 1977-08-10 |
| Donno | L | 1971-10-02 |
| Gunadi | L | 1978-08-10 |
| Maman | L | 1977-08-10 |
| Rahmat | L | 1977-03-21 |
| Sobari | L | 1976-10-02 |
| Subur | L | 1977-10-02 |
| The Cute | L | 1977-03-21 |
| Zamzam | L | 1974-08-10 |
| Zukarman | L | 1978-08-10 |
+--------------+--------+------------+
15 rows in set (0.00 sec)
-> from karyawan
-> where tgllahir < 800101
-> and jenkel="L"
-> order by nama;
+--------------+--------+------------+
| nama | jenkel | tgllahir |
+--------------+--------+------------+
| Ahmad Sobari | L | 1977-10-02 |
| Andika | L | 1978-10-02 |
| Anwar | L | 1972-10-02 |
| Beno | L | 1978-08-10 |
| Dadan | L | 1975-10-02 |
| Dadang | L | 1977-08-10 |
| Donno | L | 1971-10-02 |
| Gunadi | L | 1978-08-10 |
| Maman | L | 1977-08-10 |
| Rahmat | L | 1977-03-21 |
| Sobari | L | 1976-10-02 |
| Subur | L | 1977-10-02 |
| The Cute | L | 1977-03-21 |
| Zamzam | L | 1974-08-10 |
| Zukarman | L | 1978-08-10 |
+--------------+--------+------------+
15 rows in set (0.00 sec)
Kita lihat contohnya di bawah ini bila tanggal "1980-01-01" ditulis dengan cara "1980#01#01".
mysql>
select nama, jenkel, tgllahir
-> from karyawan
-> where tgllahir < "1980#01#01"
-> and jenkel="L"
-> order by nama ;
+--------------+--------+------------+
| nama | jenkel | tgllahir |
+--------------+--------+------------+
| Ahmad Sobari | L | 1977-10-02 |
| Andika | L | 1978-10-02 |
| Anwar | L | 1972-10-02 |
| Beno | L | 1978-08-10 |
| Dadan | L | 1975-10-02 |
| Dadang | L | 1977-08-10 |
| Donno | L | 1971-10-02 |
| Gunadi | L | 1978-08-10 |
| Maman | L | 1977-08-10 |
| Rahmat | L | 1977-03-21 |
| Sobari | L | 1976-10-02 |
| Subur | L | 1977-10-02 |
| The Cute | L | 1977-03-21 |
| Zamzam | L | 1974-08-10 |
| Zukarman | L | 1978-08-10 |
+--------------+--------+------------+
15 rows in set (0.00 sec)
-> from karyawan
-> where tgllahir < "1980#01#01"
-> and jenkel="L"
-> order by nama ;
+--------------+--------+------------+
| nama | jenkel | tgllahir |
+--------------+--------+------------+
| Ahmad Sobari | L | 1977-10-02 |
| Andika | L | 1978-10-02 |
| Anwar | L | 1972-10-02 |
| Beno | L | 1978-08-10 |
| Dadan | L | 1975-10-02 |
| Dadang | L | 1977-08-10 |
| Donno | L | 1971-10-02 |
| Gunadi | L | 1978-08-10 |
| Maman | L | 1977-08-10 |
| Rahmat | L | 1977-03-21 |
| Sobari | L | 1976-10-02 |
| Subur | L | 1977-10-02 |
| The Cute | L | 1977-03-21 |
| Zamzam | L | 1974-08-10 |
| Zukarman | L | 1978-08-10 |
+--------------+--------+------------+
15 rows in set (0.00 sec)
Kita lihat contohnya di bawah ini bila tanggal "1980-01-01" ditulis dengan cara "1980.01.01".
mysql>
select nama,jenkel, tgllahir
-> from karyawan
-> where tgllahir < "1980.01.01"
-> and jenkel="L"
-> order by nama ;
+--------------+--------+------------+
| nama | jenkel | tgllahir |
+--------------+--------+------------+
| Ahmad Sobari | L | 1977-10-02 |
| Andika | L | 1978-10-02 |
| Anwar | L | 1972-10-02 |
| Beno | L | 1978-08-10 |
| Dadan | L | 1975-10-02 |
| Dadang | L | 1977-08-10 |
| Donno | L | 1971-10-02 |
| Gunadi | L | 1978-08-10 |
| Maman | L | 1977-08-10 |
| Rahmat | L | 1977-03-21 |
| Sobari | L | 1976-10-02 |
| Subur | L | 1977-10-02 |
| The Cute | L | 1977-03-21 |
| Zamzam | L | 1974-08-10 |
| Zukarman | L | 1978-08-10 |
+--------------+--------+------------+
15 rows in set (0.00 sec)
-> from karyawan
-> where tgllahir < "1980.01.01"
-> and jenkel="L"
-> order by nama ;
+--------------+--------+------------+
| nama | jenkel | tgllahir |
+--------------+--------+------------+
| Ahmad Sobari | L | 1977-10-02 |
| Andika | L | 1978-10-02 |
| Anwar | L | 1972-10-02 |
| Beno | L | 1978-08-10 |
| Dadan | L | 1975-10-02 |
| Dadang | L | 1977-08-10 |
| Donno | L | 1971-10-02 |
| Gunadi | L | 1978-08-10 |
| Maman | L | 1977-08-10 |
| Rahmat | L | 1977-03-21 |
| Sobari | L | 1976-10-02 |
| Subur | L | 1977-10-02 |
| The Cute | L | 1977-03-21 |
| Zamzam | L | 1974-08-10 |
| Zukarman | L | 1978-08-10 |
+--------------+--------+------------+
15 rows in set (0.00 sec)
Perhatikan semua hasil di atas sama walaupun cara penulisan tanggalnya berbeda-beda (tetapi formatnya tetap mengikuti "tahun-bulan-tanggal").
Sekarang kita tampilkan data karyawan yang tanggal lahirnya
antara tanggal 1 Januari 1980 dan 31 Desember 1985, dan tampilan data diurut berdasarkan nama. Cukup hanya kolom nama, jenis
kelamin dan tanggal
lahirsaja yang ditampilkan:
mysql>
select nama, jenkel, tgllahir
-> from karyawan
-> where tgllahir >= "1980-01-01"
-> and tgllahir <= "1985-12-31"
-> order by nama ;
+------------+--------+------------+
| nama | jenkel | tgllahir |
+------------+--------+------------+
| Bambang | L | 1982-03-21 |
| Dian | P | 1980-07-07 |
| Gungun | L | 1981-03-21 |
| Mawar | P | 1985-07-07 |
| Melia | P | 1981-07-07 |
| Miranda | P | 1980-07-07 |
| Rohimat | L | 1980-03-21 |
| Ryan Cakep | L | 1981-03-21 |
| Yanti | P | 1981-06-09 |
| Yenny | P | 1985-06-09 |
| Yossy | P | 1982-06-09 |
| Yuliawati | P | 1982-06-09 |
| Zanda Cute | L | 1980-03-21 |
+------------+--------+------------+
13 rows in set (0.00 sec)
-> from karyawan
-> where tgllahir >= "1980-01-01"
-> and tgllahir <= "1985-12-31"
-> order by nama ;
+------------+--------+------------+
| nama | jenkel | tgllahir |
+------------+--------+------------+
| Bambang | L | 1982-03-21 |
| Dian | P | 1980-07-07 |
| Gungun | L | 1981-03-21 |
| Mawar | P | 1985-07-07 |
| Melia | P | 1981-07-07 |
| Miranda | P | 1980-07-07 |
| Rohimat | L | 1980-03-21 |
| Ryan Cakep | L | 1981-03-21 |
| Yanti | P | 1981-06-09 |
| Yenny | P | 1985-06-09 |
| Yossy | P | 1982-06-09 |
| Yuliawati | P | 1982-06-09 |
| Zanda Cute | L | 1980-03-21 |
+------------+--------+------------+
13 rows in set (0.00 sec)
Sekarang kita tampilkan data karyawan yang tanggal lahirnya antara tanggal 1 Januari 1980 dan 31 Desember 1985, dan tampilan data diurut berdasarkan nama. Cukup hanya ditampilkan kolom nama, jenis kelamin dantanggal lahir saja, serta hanya yang berjenis kelamin laki-laki yang ditampilkan:
mysql>
select nama, jenkel, tgllahir
-> from karyawan
-> where tgllahir >= "1980-01-01"
-> and tgllahir <= "1985-12-31"
-> and jenkel="L"
-> order by nama ;
+------------+--------+------------+
| nama | jenkel | tgllahir |
+------------+--------+------------+
| Bambang | L | 1982-03-21 |
| Gungun | L | 1981-03-21 |
| Rohimat | L | 1980-03-21 |
| Ryan Cakep | L | 1981-03-21 |
| Zanda Cute | L | 1980-03-21 |
+------------+--------+------------+
5 rows in set (0.00 sec)
-> from karyawan
-> where tgllahir >= "1980-01-01"
-> and tgllahir <= "1985-12-31"
-> and jenkel="L"
-> order by nama ;
+------------+--------+------------+
| nama | jenkel | tgllahir |
+------------+--------+------------+
| Bambang | L | 1982-03-21 |
| Gungun | L | 1981-03-21 |
| Rohimat | L | 1980-03-21 |
| Ryan Cakep | L | 1981-03-21 |
| Zanda Cute | L | 1980-03-21 |
+------------+--------+------------+
5 rows in set (0.00 sec)
Bagaimana, semakin menarik kan? Kita lanjutkan
dengan menampilkan semua data karyawan dengan usianya pada tanggal tertentu
(misal usia karyawan per 30 Agustus 2012). Untuk masalah ini memang cukup
panjang querynya. Tidak apa-apa, kita coba saja ya. Disini kita memerlukan
bantuan beberapa fungsi-fungsi yang sudah disediakan oleh MySQL. Kita lihat
dulu ya..:
mysql>
select nama, tgllahir,
-> current_date AS SEKARANG,
-> (year(current_date) - year(tgllahir))
-> - (right(current_date,5) < right(tgllahir,5)) AS USIA
-> from karyawan ;
+--------------+------------+------------+------+
| nama | tgllahir | SEKARANG | USIA |
+--------------+------------+------------+------+
| Ahmad Sobari | 1977-10-02 | 2012-08-30 | 34 |
| Sundariwati | 1978-11-12 | 2012-08-30 | 33 |
| Ryan Cakep | 1981-03-21 | 2012-08-30 | 31 |
| Zukarman | 1978-08-10 | 2012-08-30 | 34 |
| Yuliawati | 1982-06-09 | 2012-08-30 | 25 |
| Mawar | 1985-07-07 | 2012-08-30 | 27 |
| Sobari | 1976-10-02 | 2012-08-30 | 35 |
| Melia | 1979-11-12 | 2012-08-30 | 32 |
| Zanda Cute | 1980-03-21 | 2012-08-30 | 32 |
| Maman | 1977-08-10 | 2012-08-30 | 35 |
| Yenny | 1985-06-09 | 2012-08-30 | 27 |
| Rossa | 1987-07-07 | 2012-08-30 | 25 |
| Dadan | 1975-10-02 | 2012-08-30 | 36 |
| Wawan | 1971-11-12 | 2012-08-30 | 40 |
| The Cute | 1977-03-21 | 2012-08-30 | 35 |
| Marpaung | 1988-08-10 | 2012-08-30 | 24 |
| Yono | 1989-06-09 | 2012-08-30 | 23 |
| Dian | 1980-07-07 | 2012-08-30 | 32 |
| Donno | 1971-10-02 | 2012-08-30 | 45 |
| Ratu | 1972-11-12 | 2012-08-30 | 39 |
| Bambang | 1982-03-21 | 2012-08-30 | 30 |
| Dadang | 1977-08-10 | 2012-08-30 | 35 |
| Yuliawati | 1974-06-09 | 2012-08-30 | 38 |
| Miranda | 1980-07-07 | 2012-08-30 | 32 |
| Subur | 1977-10-02 | 2012-08-30 | 34 |
| Banowati | 1978-11-12 | 2012-08-30 | 33 |
| Gungun | 1981-03-21 | 2012-08-30 | 31 |
| Gunadi | 1978-08-10 | 2012-08-30 | 34 |
| Yossy | 1982-06-09 | 2012-08-30 | 30 |
| Melia | 1981-07-07 | 2012-08-30 | 31 |
| Anwar | 1972-10-02 | 2012-08-30 | 39 |
| Susilowati | 1973-11-12 | 2012-08-30 | 38 |
| Rahmat | 1977-03-21 | 2012-08-30 | 35 |
| Zamzam | 1974-08-10 | 2012-08-30 | 38 |
| Nenny | 1972-06-09 | 2012-08-30 | 40 |
| Mardiatun | 1975-07-07 | 2012-08-30 | 37 |
| Andika | 1978-10-02 | 2012-08-30 | 33 |
| Siti | 1988-11-12 | 2012-08-30 | 23 |
| Rohimat | 1980-03-21 | 2012-08-30 | 32 |
| Beno | 1978-08-10 | 2012-08-30 | 34 |
| Yanti | 1981-06-09 | 2012-08-30 | 31 |
| Miranti | 1975-07-07 | 2012-08-30 | 37 |
+--------------+------------+------------+------+
42 rows in set (0.00 sec)
-> current_date AS SEKARANG,
-> (year(current_date) - year(tgllahir))
-> - (right(current_date,5) < right(tgllahir,5)) AS USIA
-> from karyawan ;
+--------------+------------+------------+------+
| nama | tgllahir | SEKARANG | USIA |
+--------------+------------+------------+------+
| Ahmad Sobari | 1977-10-02 | 2012-08-30 | 34 |
| Sundariwati | 1978-11-12 | 2012-08-30 | 33 |
| Ryan Cakep | 1981-03-21 | 2012-08-30 | 31 |
| Zukarman | 1978-08-10 | 2012-08-30 | 34 |
| Yuliawati | 1982-06-09 | 2012-08-30 | 25 |
| Mawar | 1985-07-07 | 2012-08-30 | 27 |
| Sobari | 1976-10-02 | 2012-08-30 | 35 |
| Melia | 1979-11-12 | 2012-08-30 | 32 |
| Zanda Cute | 1980-03-21 | 2012-08-30 | 32 |
| Maman | 1977-08-10 | 2012-08-30 | 35 |
| Yenny | 1985-06-09 | 2012-08-30 | 27 |
| Rossa | 1987-07-07 | 2012-08-30 | 25 |
| Dadan | 1975-10-02 | 2012-08-30 | 36 |
| Wawan | 1971-11-12 | 2012-08-30 | 40 |
| The Cute | 1977-03-21 | 2012-08-30 | 35 |
| Marpaung | 1988-08-10 | 2012-08-30 | 24 |
| Yono | 1989-06-09 | 2012-08-30 | 23 |
| Dian | 1980-07-07 | 2012-08-30 | 32 |
| Donno | 1971-10-02 | 2012-08-30 | 45 |
| Ratu | 1972-11-12 | 2012-08-30 | 39 |
| Bambang | 1982-03-21 | 2012-08-30 | 30 |
| Dadang | 1977-08-10 | 2012-08-30 | 35 |
| Yuliawati | 1974-06-09 | 2012-08-30 | 38 |
| Miranda | 1980-07-07 | 2012-08-30 | 32 |
| Subur | 1977-10-02 | 2012-08-30 | 34 |
| Banowati | 1978-11-12 | 2012-08-30 | 33 |
| Gungun | 1981-03-21 | 2012-08-30 | 31 |
| Gunadi | 1978-08-10 | 2012-08-30 | 34 |
| Yossy | 1982-06-09 | 2012-08-30 | 30 |
| Melia | 1981-07-07 | 2012-08-30 | 31 |
| Anwar | 1972-10-02 | 2012-08-30 | 39 |
| Susilowati | 1973-11-12 | 2012-08-30 | 38 |
| Rahmat | 1977-03-21 | 2012-08-30 | 35 |
| Zamzam | 1974-08-10 | 2012-08-30 | 38 |
| Nenny | 1972-06-09 | 2012-08-30 | 40 |
| Mardiatun | 1975-07-07 | 2012-08-30 | 37 |
| Andika | 1978-10-02 | 2012-08-30 | 33 |
| Siti | 1988-11-12 | 2012-08-30 | 23 |
| Rohimat | 1980-03-21 | 2012-08-30 | 32 |
| Beno | 1978-08-10 | 2012-08-30 | 34 |
| Yanti | 1981-06-09 | 2012-08-30 | 31 |
| Miranti | 1975-07-07 | 2012-08-30 | 37 |
+--------------+------------+------------+------+
42 rows in set (0.00 sec)
Kita lanjutkan dengan menampilkan data karyawan yang usianya sama atau dibawah 30 tahun (per 30 Agustus 2012). Nah bagaimana caranya?:
mysql>
select nama, tgllahir,
-> current_date AS SEKARANG,
-> (year(current_date) - year(tgllahir))
-> - (right(current_date,5) < right(tgllahir,5))
-> AS USIA
-> from karyawan
-> where ((year(current_date) - year(tgllahir))
-> - (right(current_date,5) < right(tgllahir,5)))
-> <= 25 ;
+-----------+------------+------------+------+
| nama | tgllahir | SEKARANG | USIA |
+-----------+------------+------------+------+
| Yuliawati | 1982-06-09 | 2012-08-30 | 30 |
| Mawar | 1985-07-07 | 2012-08-30 | 27 |
| Yenny | 1985-06-09 | 2012-08-30 | 27 |
| Rossa | 1987-07-07 | 2012-08-30 | 25 |
| Marpaung | 1988-08-10 | 2012-08-30 | 24 |
| Yono | 1989-06-09 | 2012-08-30 | 23 |
| Bambang | 1982-03-21 | 2012-08-30 | 30 |
| Yossy | 1982-06-09 | 2012-08-30 | 30 |
| Siti | 1988-11-12 | 2012-08-30 | 23 |
+-----------+------------+------------+------+
9 rows in set (0.00 sec)
-> current_date AS SEKARANG,
-> (year(current_date) - year(tgllahir))
-> - (right(current_date,5) < right(tgllahir,5))
-> AS USIA
-> from karyawan
-> where ((year(current_date) - year(tgllahir))
-> - (right(current_date,5) < right(tgllahir,5)))
-> <= 25 ;
+-----------+------------+------------+------+
| nama | tgllahir | SEKARANG | USIA |
+-----------+------------+------------+------+
| Yuliawati | 1982-06-09 | 2012-08-30 | 30 |
| Mawar | 1985-07-07 | 2012-08-30 | 27 |
| Yenny | 1985-06-09 | 2012-08-30 | 27 |
| Rossa | 1987-07-07 | 2012-08-30 | 25 |
| Marpaung | 1988-08-10 | 2012-08-30 | 24 |
| Yono | 1989-06-09 | 2012-08-30 | 23 |
| Bambang | 1982-03-21 | 2012-08-30 | 30 |
| Yossy | 1982-06-09 | 2012-08-30 | 30 |
| Siti | 1988-11-12 | 2012-08-30 | 23 |
+-----------+------------+------------+------+
9 rows in set (0.00 sec)
Cukup panjang perintahnya ya. Disini kita menggunakan fungsi CURRENT_DATE yang mengambil nilai dari tanggal saat ini pada sistem komputer Anda. YEAR adalah fungsi yang mengambil nilai tahun. Kemudian ASadalah singkatan dari AliaS, yang seolah-olah memberikan nama lain (alias name) pada kolom atau pada hasil suatu proses.
Sedangkan RIGHT adalah fungsi yang mengambil nilai dari sekian karakter dari sisi kanan sebuah target. MisalRIGHT("TOMAT", 3) maka akan menghasilkan karakter "MAT".
Keterangan lengkap dari fungsi-fungsi MySQL ini dapat Anda baca pada file Manual MySQL secara online dihttp://dev.mysql.com/.
0 comments:
Post a Comment