ใช้ SUM() ร่วมกับการคูณคอลัมน์ภายในฟังก์ชัน
สร้างตารางสินค้า
mysql> create table DemoTable(
ProductQuantity int,
ProductPrice int );
Query OK, 0 rows affected (0.48 sec)
เพิ่มข้อมูล
mysql> insert into DemoTable values(10,9); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(6,20); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(7,100); Query OK, 1 row affected (0.08 sec)
แสดงข้อมูล
mysql> select * from DemoTable;
+-----------------+--------------+ | ProductQuantity | ProductPrice | +-----------------+--------------+ | 10 | 9 | | 6 | 20 | | 7 | 100 | +-----------------+--------------+ 3 rows in set (0.00 sec)
คิวรีคูณจำนวน × ราคา แล้วรวมผลรวม
mysql> select SUM(ProductQuantity * ProductPrice) AS Total_Value from DemoTable;
+-------------+ | Total_Value | +-------------+ | 910 | +-------------+ 1 row in set (0.00 sec)
การคำนวณ: (10×9) + (6×20) + (7×100) = 90 + 120 + 700 = 910