Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

แสดงผลรวม (SUM) ในแถวสุดท้ายของตารางด้วย UNION ใน MySQL


แนวคิด

ใช้ UNION รวมผลลัพธ์ของข้อมูลปกติ กับแถวสรุปยอดรวม (Total) ไว้ใน result set เดียว

รูปแบบคำสั่ง

(SELECT col1, col2 FROM table)
UNION
(SELECT 'Total' AS col1, SUM(col2) FROM table);

ตัวอย่างครบถ้วน

1. สร้างตารางคะแนนนักเรียน

mysql> create table showSumInLastRowDemo 
    -> ( 
    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    -> StudentName varchar(20), 
    -> StudentMarks int 
    -> ); 
Query OK, 0 rows affected (0.69 sec)

2. เพิ่มข้อมูล (มีนักเรียนซ้ำชื่อ)

mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('John',56); 
Query OK, 1 row affected (0.14 sec)
mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('John',87); 
Query OK, 1 row affected (0.10 sec)
mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('John',52); 
Query OK, 1 row affected (0.17 sec)
mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('Carol',97); 
Query OK, 1 row affected (0.12 sec)
mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('Larry',75); 
Query OK, 1 row affected (0.14 sec)
mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('Larry',98); 
Query OK, 1 row affected (0.10 sec)
mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('Carol',73); 
Query OK, 1 row affected (0.14 sec)

3. ดูข้อมูลทั้งหมด

mysql> select * from showSumInLastRowDemo;
+-----------+-------------+--------------+
| StudentId | StudentName | StudentMarks |
+-----------+-------------+--------------+
| 1         | John        | 56           |
| 2         | John        | 87           |
| 3         | John        | 52           |
| 4         | Carol       | 97           |
| 5         | Larry       | 75           |
| 6         | Larry       | 98           |
| 7         | Carol       | 73           |
+-----------+-------------+--------------+

4. UNION กับแถว Total

mysql> (select StudentName, StudentMarks from showSumInLastRowDemo) 
    -> UNION 
    -> (select 'TotalMarksOfAllStudent' as StudentName, sum(StudentMarks) as StudentMarks from showSumInLastRowDemo);
+------------------------+--------------+
| StudentName            | StudentMarks |
+------------------------+--------------+
| John                   | 56           |
| John                   | 87           |
| John                   | 52           |
| Carol                  | 97           |
| Larry                  | 75           |
| Larry                  | 98           |
| Carol                  | 73           |
| TotalMarksOfAllStudent | 538          |
+------------------------+--------------+

ข้อควรระวัง

  • UNION ลบแถวซ้ำ → ถ้ามีคนชื่อ "TotalMarksOfAllStudent" จะหายไป (ใช้ UNION ALL ป้องกัน)
  • จำนวนคอลัมน์และชนิดข้อมูลต้องตรงกันทั้ง 2 ฝั่ง
  • ORDER BY ไม่รับประกันลำดับ → ใช้ subquery หรือเพิ่ม sort column

เวอร์ชันที่ดีกว่า: ใช้ UNION ALL + Sort Column

(SELECT 1 AS sort_order, StudentName, StudentMarks FROM showSumInLastRowDemo)
UNION ALL
(SELECT 2 AS sort_order, 'Total' AS StudentName, SUM(StudentMarks) FROM showSumInLastRowDemo)
ORDER BY sort_order, StudentName;

MySQL 8.0+: ใช้ WITH ROLLUP

SELECT 
  COALESCE(StudentName, 'Grand Total') AS StudentName,
  SUM(StudentMarks) AS TotalMarks
FROM showSumInLastRowDemo
GROUP BY StudentName WITH ROLLUP;
+----------------+------------+
| StudentName    | TotalMarks |
+----------------+------------+
| Carol          | 170        |
| John           | 195        |
| Larry          | 173        |
| Grand Total    | 538        |
+----------------+------------+

สรุป: WITH ROLLUP ทันสมัยกว่า ให้ subtotal และ grand total อัตโนมัติ