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

รวม SUM คอลัมน์จากหลายตารางใน MySQL ด้วย UNION ALL


แนวคิด

ใช้ UNION ALL รวมค่าจากหลายตาราง แล้ว SUM() รวมทั้งหมด (UNION ALL เก็บค่าซ้ำ ไม่ลบออก)

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

SELECT SUM(tbl.col) AS TotalPrice 
FROM (
  SELECT col FROM table1
  UNION ALL
  SELECT col FROM table2
  -- UNION ALL table3, table4...
) tbl;

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

1. สร้างตาราง Products1

mysql> create table Products1 
    -> ( 
    -> ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    -> ProductName varchar(20), 
    -> ProductPrice int 
    -> ); 
Query OK, 0 rows affected (0.50 sec)

2. เพิ่มข้อมูล Products1

mysql> insert into Products1(ProductName,ProductPrice) values('Product-1',100); 
Query OK, 1 row affected (0.22 sec)
mysql> insert into Products1(ProductName,ProductPrice) values('Product-2',200); 
Query OK, 1 row affected (0.34 sec)
mysql> insert into Products1(ProductName,ProductPrice) values('Product-3',700); 
Query OK, 1 row affected (0.15 sec)
mysql> select * from Products1;
+-----------+-------------+--------------+
| ProductId | ProductName | ProductPrice |
+-----------+-------------+--------------+
| 1         | Product-1   | 100          |
| 2         | Product-2   | 200          |
| 3         | Product-3   | 700          |
+-----------+-------------+--------------+

3. สร้างตาราง Products2

mysql> create table Products2 
    -> ( 
    -> ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    -> ProductName varchar(20), 
    -> ProductPrice int 
    -> ); 
Query OK, 0 rows affected (0.63 sec)

4. เพิ่มข้อมูล Products2

mysql> insert into Products2(ProductName,ProductPrice) values('Product-1',500); 
Query OK, 1 row affected (0.24 sec)
mysql> insert into Products2(ProductName,ProductPrice) values('Product-4',100); 
Query OK, 1 row affected (0.13 sec)
mysql> insert into Products2(ProductName,ProductPrice) values('Product-5',400); 
Query OK, 1 row affected (0.10 sec)
mysql> select * from Products2;
+-----------+-------------+--------------+
| ProductId | ProductName | ProductPrice |
+-----------+-------------+--------------+
| 1         | Product-1   | 500          |
| 2         | Product-4   | 100          |
| 3         | Product-5   | 400          |
+-----------+-------------+--------------+

5. SUM รวมจากทั้ง 2 ตาราง

mysql> SELECT SUM(tbl.ProductPrice) AS TotalPrice 
    -> FROM (
    ->   SELECT ProductPrice FROM Products1
    ->   UNION ALL
    ->   SELECT ProductPrice FROM Products2
    -> ) tbl;
+------------+
| TotalPrice |
+------------+
| 2000       |
+------------+

การคำนวณยืนยัน

ตารางProductPriceSum
Products1100, 200, 7001000
Products2500, 100, 4001000
รวม2000

ขยาย: หลายตารางมากกว่า 2

SELECT SUM(tbl.price) AS grand_total
FROM (
  SELECT price FROM sales_2023
  UNION ALL SELECT price FROM sales_2022
  UNION ALL SELECT price FROM sales_2021
  UNION ALL SELECT price FROM sales_2020
) tbl;

ทำไมต้อง UNION ALL ไม่ใช่ UNION?

  • UNION ลบ duplicate rows → ผลรวมจะน้อยไปถ้ามีราคาเท่ากัน
  • UNION ALL เก็บทุกแถว → ผลรวมถูกต้องเสมอ + เร็วกว่า (ไม่ต้อง distinct)

สรุป: UNION ALL + SUM = วิธีมาตรฐานรวมยอดข้ามตารางใน SQL