คูณจำนวนสินค้าด้วยราคา แล้วรวมทั้งหมดด้วย SUM()
สร้างตารางและเพิ่มข้อมูล
mysql> CREATE TABLE DemoTable (
ProductId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ProductQuantity INT,
ProductPrice INT
);
mysql> INSERT INTO DemoTable(ProductQuantity, ProductPrice) VALUES
(10, 100), (5, 11), (3, 140), (2, 450);
ดูข้อมูล
mysql> SELECT * FROM DemoTable;
+-----------+-----------------+--------------+
| ProductId | ProductQuantity | ProductPrice |
+-----------+-----------------+--------------+
| 1 | 10 | 100 |
| 2 | 5 | 11 |
| 3 | 3 | 140 |
| 4 | 2 | 450 |
+-----------+-----------------+--------------+
คำนวณมูลค่ารวม
mysql> SELECT SUM(ProductQuantity * ProductPrice) AS Total_of_Products
-> FROM DemoTable;
ผลลัพธ์
+-------------------+
| Total_of_Products |
+-------------------+
| 2375 |
+-------------------+
การคำนวณ: (10×100) + (5×11) + (3×140) + (2×450) = 1000 + 55 + 420 + 900 = 2375