MySQL কোয়েরি রেজাল্টের শেষে সারির যোগফল (Total) দেখাতে UNION ব্যবহার করুন।
সিনট্যাক্স
(SELECT col1, col2, col3 FROM yourTable) UNION ALL (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), ('Product-2', 5, 200),
('Product-3', 7, 340), ('Product-4', 20, 500),
('Product-5', 30, 1000);
টোটালসহ কোয়েরি
(SELECT ProductId, ProductQuantity, ProductValue FROM ProductDemo)
UNION ALL
(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 ALL ব্যবহার করুন (দ্রুত, ডুপ্লিকেট চেক করে না)। কলাম সংখ্যা এবং টাইপ মিলতে হবে।