MySQL में मात्रा × मूल्य का योग से कुल उत्पाद मूल्य निकालें
आइए पहले एक टेबल बनाते हैं:
mysql> create table DemoTable
(
ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
ProductQuantity int,
ProductPrice int
); Query OK, 0 rows affected (0.19 sec)
इन्सर्ट कमांड से टेबल में कुछ रिकॉर्ड डालें:
mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(10,100); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(5,11); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(3,140); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(2,450); Query OK, 1 row affected (0.15 sec)
सेलेक्ट स्टेटमेंट से टेबल के सभी रिकॉर्ड प्रदर्शित करें:
mysql> select *from DemoTable;
आउटपुट:
+-----------+-----------------+--------------+ | ProductId | ProductQuantity | ProductPrice | +-----------+-----------------+--------------+ | 1 | 10 | 100 | | 2 | 5 | 11 | | 3 | 3 | 140 | | 4 | 2 | 450 | +-----------+-----------------+--------------+ 4 rows in set (0.00 sec)
उत्पादों के कुल मूल्य की गणना करने का क्वेरी:
mysql> select sum(ProductQuantity*ProductPrice) as Total_of_Products from DemoTable;
आउटपुट:
+-------------------+ | Total_of_Products | +-------------------+ | 2375 | +-------------------+ 1 row in set (0.00 sec)
गणना: (10×100) + (5×11) + (3×140) + (2×450) = 1000 + 55 + 420 + 900 = 2375।
नोट: यह क्वेरी प्रत्येक पंक्ति के लिए मात्रा × मूल्य की गणना करती है, फिर सभी का योग करती है। COALESCE का उपयोग करें यदि NULL मान संभव हैं।