MySQL में दो कॉलम गुणा करके उनके परिणाम का योग निकालने के लिए SUM() के साथ गुणा संक्रिया का उपयोग करें।
टेबल निर्माण
mysql> CREATE TABLE DemoTable (
CustomerId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
CustomerProductName VARCHAR(100),
CustomerProductQuantity INT,
CustomerPrice INT
);
डेटा डालना
mysql> INSERT INTO DemoTable(CustomerProductName, CustomerProductQuantity, CustomerPrice)
VALUES('Product-1', 5, 400);
mysql> INSERT INTO DemoTable(CustomerProductName, CustomerProductQuantity, CustomerPrice)
VALUES('Product-2', 3, 100);
mysql> INSERT INTO DemoTable(CustomerProductName, CustomerProductQuantity, CustomerPrice)
VALUES('Product-1', 2, 300);
mysql> INSERT INTO DemoTable(CustomerProductName, CustomerProductQuantity, CustomerPrice)
VALUES('Product-1', 5, 50);
mysql> INSERT INTO DemoTable(CustomerProductName, CustomerProductQuantity, CustomerPrice)
VALUES('Product-3', 6, 10);
mysql> INSERT INTO DemoTable(CustomerProductName, CustomerProductQuantity, CustomerPrice)
VALUES('Product-2', 10, 20);
सभी रिकॉर्ड देखें
mysql> SELECT * FROM DemoTable;
+------------+---------------------+-------------------------+---------------+ | CustomerId | CustomerProductName | CustomerProductQuantity | CustomerPrice | +------------+---------------------+-------------------------+---------------+ | 1 | Product-1 | 5 | 400 | | 2 | Product-2 | 3 | 100 | | 3 | Product-1 | 2 | 300 | | 4 | Product-1 | 5 | 50 | | 5 | Product-3 | 6 | 10 | | 6 | Product-2 | 10 | 20 | +------------+---------------------+-------------------------+---------------+ 6 rows in set (0.00 sec)
उत्पादवार कुल कीमत निकालने की क्वेरी
mysql> SELECT CustomerProductName,
SUM(CustomerProductQuantity * CustomerPrice) AS TOTAL_PRICE
FROM DemoTable
GROUP BY CustomerProductName;
परिणाम
+---------------------+-------------+ | CustomerProductName | TOTAL_PRICE | +---------------------+-------------+ | Product-1 | 2850 | | Product-2 | 500 | | Product-3 | 60 | +---------------------+-------------+ 3 rows in set (0.00 sec)
गणना: Product-1: (5×400)+(2×300)+(5×50) = 2000+600+250 = 2850