Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

ลดราคา 20% จากราคาที่รวมภาษีแล้วใน MySQL

ราคารวมภาษี 20% → ราคาต้นทุน = ราคารวม / 1.2

สร้างตารางและเพิ่มราคา

mysql> create table DemoTable ( Price int ); 
Query OK, 0 rows affected (1.09 sec)

mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.07 sec)
mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.18 sec)

อัปเดตลด 20% (หาร 1.2)

mysql> update DemoTable set Price = Price / 1.2; 
Query OK, 4 rows affected (0.11 sec)

ผลลัพธ์ (ปัดเศษเป็นจำนวนเต็ม)

mysql> select * from DemoTable;
+-------+
| Price |
+-------+
| 17    |
| 33    |
| 67    |
| 50    |
+-------+
4 rows in set (0.00 sec)

การคำนวณ: 20/1.2=16.67→17, 40/1.2=33.33→33, 80/1.2=66.67→67, 60/1.2=50

ข้อควรระวัง: INT จะปัดทิ้งทศนิยม หากต้องการทศนิยม ใช้ DECIMAL(10,2)