MySQL क्वेरी परिणाम के अंत में कुल पंक्ति जोड़ने के लिए UNION का उपयोग करें।
सिंटैक्स:
(SELECT col1, col2, col3 FROM yourTable) UNION (SELECT 'Total' AS col1, SUM(col2) AS col2, SUM(col3) AS col3 FROM yourTable);
एक टेबल बनाते हैं:
CREATE TABLE ProductDemo (
ProductId VARCHAR(10),
ProductQuantity INT,
ProductValue INT
);
रिकॉर्ड डालें:
INSERT INTO ProductDemo VALUES ('Product-1', 10, 300);
INSERT INTO ProductDemo VALUES ('Product-2', 5, 200);
INSERT INTO ProductDemo VALUES ('Product-3', 7, 340);
INSERT INTO ProductDemo VALUES ('Product-4', 20, 500);
INSERT INTO ProductDemo VALUES ('Product-5', 30, 1000);
कुल पंक्ति के साथ परिणाम:
(SELECT ProductId, ProductQuantity, ProductValue FROM ProductDemo) UNION (SELECT 'Total' AS ProductId, SUM(ProductQuantity) AS ProductQuantity, SUM(ProductValue) AS ProductValue FROM ProductDemo);
आउटपुट
+-----------+-----------------+--------------+ | ProductId | ProductQuantity | ProductValue | +-----------+-----------------+--------------+ | Product-1 | 10 | 300 | | Product-2 | 5 | 200 | | Product-3 | 7 | 340 | | Product-4 | 20 | 500 | | Product-5 | 30 | 1000 | | Total | 72 | 2340 | +-----------+-----------------+--------------+
नोट: UNION डुप्लिकेट हटाता है; यदि डुप्लिकेट रखने हों तो UNION ALL उपयोग करें। कॉलम संख्या और डेटा प्रकार मेल खाने चाहिए।