Macam-Macam Operator Aritmatika
Berikut ini tabel macam-macam operator aritmatika yang terdapat pada MySQL:
Operator | Deskripsi | Contoh |
---|---|---|
+ | Operator penambahan | 30 + 20 |
- | Operator pengurangan | 30 - 20 |
* | Operator perkalian | 30 * 20 |
/ | Operator pembagian | 30 / 20 |
% | Operator modulus (sisa bagi) | 30 % 20 |
Contoh
Berikut ini contoh implementasi operator aritmatika pada Select statement:
Contoh #1
mysql> SELECT 30+20;
+-------+
| 30+20 |
+-------+
| 50 |
+-------+
1 row in set (0.04 sec)
mysql> SELECT 30-20;
+-------+
| 30-20 |
+-------+
| 10 |
+-------+
1 row in set (0.06 sec)
mysql> SELECT 30*20;
+-------+
| 30*20 |
+-------+
| 600 |
+-------+
1 row in set (0.00 sec)
mysql> SELECT 30/20;
+--------+
| 30/20 |
+--------+
| 1.5000 |
+--------+
1 row in set (0.00 sec)
mysql> SELECT 30%20;
+-------+
| 30%20 |
+-------+
| 10 |
+-------+
1 row in set (0.00 sec)
Contoh #2
Misalkan Anda memiliki sebuah table "tbl_employee" dengan data-data sebagai berikut:
mysql> SELECT * FROM tbl_employee;Dari data diatas, untuk pegawai yang berada di department 'IT', tambahkan gajinya sebesar 500 ribu. dan Tampikan kembali data dari table diatas.
+-------------+---------------+------------+-------------+------------+------------+--------+
| employee_id | employee_name | birth_date | birth_place | salary | department | gender |
+-------------+---------------+------------+-------------+------------+------------+--------+
| 1 | Ahmad Sofyan | 1990-01-01 | Bandung | 1750000.00 | IT | L |
| 2 | Ismail Saleh | 1986-12-12 | Pandeglang | 5000000.00 | ACC | L |
| 3 | Ferry Subekti | 1987-06-06 | Purwakarta | 4500000.00 | HRD | P |
| 4 | Iskiyati | 1988-07-07 | Brebes | 7500000.00 | FIN | P |
| 5 | Nurul Hikmah | 1989-08-08 | Purwokerto | 3750000.00 | FIN | P |
| 6 | Riyanto | 1983-02-02 | Subang | 4000000.00 | IT | L |
| 7 | Azka Nurul | 1984-03-03 | Bandung | 5000000.00 | HRD | P |
| 8 | Venny Maya | 1989-04-04 | Jakarta | 6500000.00 | IT | P |
| 9 | Ana Fauziyah | 1989-05-05 | Bekasi | 3700000.00 | ACC | P |
| 10 | Arya Adhi | 1980-09-09 | Magelang | 8000000.00 | FIN | P |
+-------------+---------------+------------+-------------+------------+------------+--------+
10 rows in set (0.00 sec)
Untuk menyelesaikan soal diatas, Anda dapat menggunakan operator aritmatika "+" pada Update statement seperti berikut ini:
mysql> UPDATE tbl_employee
-> SET salary = salary + 500000
-> WHERE department = 'IT';
Query OK, 3 rows affected (0.10 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> SELECT * FROM tbl_employee;
+-------------+---------------+------------+-------------+------------+------------+--------+
| employee_id | employee_name | birth_date | birth_place | salary | department | gender |
+-------------+---------------+------------+-------------+------------+------------+--------+
| 1 | Ahmad Sofyan | 1990-01-01 | Bandung | 2250000.00 | IT | L |
| 2 | Ismail Saleh | 1986-12-12 | Pandeglang | 5000000.00 | ACC | L |
| 3 | Ferry Subekti | 1987-06-06 | Purwakarta | 4500000.00 | HRD | P |
| 4 | Iskiyati | 1988-07-07 | Brebes | 7500000.00 | FIN | P |
| 5 | Nurul Hikmah | 1989-08-08 | Purwokerto | 3750000.00 | FIN | P |
| 6 | Riyanto | 1983-02-02 | Subang | 4500000.00 | IT | L |
| 7 | Azka Nurul | 1984-03-03 | Bandung | 5000000.00 | HRD | P |
| 8 | Venny Maya | 1989-04-04 | Jakarta | 7000000.00 | IT | P |
| 9 | Ana Fauziyah | 1989-05-05 | Bekasi | 3700000.00 | ACC | P |
| 10 | Arya Adhi | 1980-09-09 | Magelang | 8000000.00 | FIN | P |
+-------------+---------------+------------+-------------+------------+------------+--------+
10 rows in set (0.00 sec)
Sekian tutorial singkat tentang Pengenalan Operator Aritmatika Pada MySQL. Semoga bermanfaat & Happy Learning MySQL Database.
Salam,
Nursalim
Bagikan
Pengenalan Operator Aritmatika Pada MySQL
4/
5
Oleh
Kris Kimcil