Làm cách nào để tính tổng nhiều cột trong MySQL?

Để chọn nhiều cột tổng bằng truy vấn MySQL và hiển thị chúng trong các cột riêng biệt, bạn cần sử dụng câu lệnh CASE. Cú pháp như sau

Show
SELECT
SUM( CASE WHEN yourColumnName1=’yourValue1’ THEN yourColumnName2 END ) AS yourSeparateColumnName1,
SUM( CASE WHEN yourColumnName1=’yourValue2’ THEN yourColumnName2 END ) AS yourSeparateColumnName2,
SUM( CASE WHEN yourColumnName1=’yourValue3’ THEN yourColumnName2 END ) AS yourSeparateColumnName3,
.
.
.
N
FROM yourTableName;

Để hiểu cú pháp trên, chúng ta hãy tạo một bảng. Truy vấn để tạo bảng như sau

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)

Bây giờ bạn có thể chèn một số bản ghi vào bảng bằng lệnh chèn. Truy vấn như sau

mysql> insert into selectMultipleSumDemo(PlayerName,PlayerScore) values('Maxwell',89);
Query OK, 1 row affected (0.23 sec)
mysql> insert into selectMultipleSumDemo(PlayerName,PlayerScore) values('Ricky',98);
Query OK, 1 row affected (0.15 sec)
mysql> insert into selectMultipleSumDemo(PlayerName,PlayerScore) values('Maxwell',96);
Query OK, 1 row affected (0.18 sec)
mysql> insert into selectMultipleSumDemo(PlayerName,PlayerScore) values('Ricky',78);
Query OK, 1 row affected (0.16 sec)
mysql> insert into selectMultipleSumDemo(PlayerName,PlayerScore) values('Maxwell',51);
Query OK, 1 row affected (0.17 sec)
mysql> insert into selectMultipleSumDemo(PlayerName,PlayerScore) values('Ricky',89);
Query OK, 1 row affected (0.21 sec)
mysql> insert into selectMultipleSumDemo(PlayerName,PlayerScore) values('David',56);
Query OK, 1 row affected (0.15 sec)
mysql> insert into selectMultipleSumDemo(PlayerName,PlayerScore) values('David',65);
Query OK, 1 row affected (0.19 sec)

Hiển thị tất cả các bản ghi từ bảng bằng cách sử dụng câu lệnh chọn. Truy vấn như sau

mysql> select *from selectMultipleSumDemo;

Sau đây là đầu ra

+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)

Truy vấn để lấy một cột riêng có nhiều tổng

mysql> select
   -> SUM(CASE WHEN PlayerName='Maxwell' THEN PlayerScore END) AS 'MAXWELL TOTAL SCORE',
   -> SUM(CASE WHEN PlayerName='Ricky' THEN PlayerScore END) AS 'RICKY TOTAL SCORE',
   -> SUM(CASE WHEN PlayerName='David' THEN PlayerScore END) AS 'DAVID TOTAL SCORE'
   -> from selectMultipleSumDemo;

Sau đây là đầu ra

+---------------------+-------------------+-------------------+
| MAXWELL TOTAL SCORE | RICKY TOTAL SCORE | DAVID TOTAL SCORE |
+---------------------+-------------------+-------------------+
|                 236 |               265 |               121 |
+---------------------+-------------------+-------------------+
1 row in set (0.00 sec)

Bảng có các cột này

id | name1 | score1 | name2 | score2

Tôi cần chuyển đổi kết quả của hai truy vấn này thành một

________số 8

Kết quả là '

id |   name1  |  score1  |   name2  |  score2
1  | james    | 5        | carolina | 3
2  | carolina | 3        | troll    | 9
3  | mordor   | 6        | ent      | 5
4  | carolina | 1        | paul     | 3
5  | paul     | 18       | kek      | 1
2', tôi cần tổng này và tên trong một truy vấn

Ví dụ

id |   name1  |  score1  |   name2  |  score2
1  | james    | 5        | carolina | 3
2  | carolina | 3        | troll    | 9
3  | mordor   | 6        | ent      | 5
4  | carolina | 1        | paul     | 3
5  | paul     | 18       | kek      | 1

Kết quả

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
0- stackoverflow. com

ghi bàn. 1

câu trả lời được chấp nhận

sử dụng

id |   name1  |  score1  |   name2  |  score2
1  | james    | 5        | carolina | 3
2  | carolina | 3        | troll    | 9
3  | mordor   | 6        | ent      | 5
4  | carolina | 1        | paul     | 3
5  | paul     | 18       | kek      | 1
3 để kết hợp
id |   name1  |  score1  |   name2  |  score2
1  | james    | 5        | carolina | 3
2  | carolina | 3        | troll    | 9
3  | mordor   | 6        | ent      | 5
4  | carolina | 1        | paul     | 3
5  | paul     | 18       | kek      | 1
4 và
id |   name1  |  score1  |   name2  |  score2
1  | james    | 5        | carolina | 3
2  | carolina | 3        | troll    | 9
3  | mordor   | 6        | ent      | 5
4  | carolina | 1        | paul     | 3
5  | paul     | 18       | kek      | 1
5 trong một cột, sau đó thực hiện
id |   name1  |  score1  |   name2  |  score2
1  | james    | 5        | carolina | 3
2  | carolina | 3        | troll    | 9
3  | mordor   | 6        | ent      | 5
4  | carolina | 1        | paul     | 3
5  | paul     | 18       | kek      | 1
6

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
1

Thêm câu hỏi với thẻ tương tự

Tóm lược. trong hướng dẫn này, bạn sẽ học cách sử dụng hàm MySQL

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 để tính tổng các giá trị trong một tập hợp

Giới thiệu về hàm MySQL mysql> create table selectMultipleSumDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> PlayerName varchar(20),    -> PlayerScore int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.58 sec)25

Hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 là một hàm tổng hợp cho phép bạn tính tổng các giá trị trong một tập hợp. Cú pháp của hàm
mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 như sau

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
6

Đây là cách hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 hoạt động

  • Nếu bạn sử dụng hàm
    mysql> create table selectMultipleSumDemo
       -> (
       -> Id int NOT NULL AUTO_INCREMENT,
       -> PlayerName varchar(20),
       -> PlayerScore int,
       -> PRIMARY KEY(Id)
       -> );
    Query OK, 0 rows affected (0.58 sec)
    25 trong câu lệnh
    mysql> create table selectMultipleSumDemo
       -> (
       -> Id int NOT NULL AUTO_INCREMENT,
       -> PlayerName varchar(20),
       -> PlayerScore int,
       -> PRIMARY KEY(Id)
       -> );
    Query OK, 0 rows affected (0.58 sec)
    91 không trả về hàng nào, thì hàm
    mysql> create table selectMultipleSumDemo
       -> (
       -> Id int NOT NULL AUTO_INCREMENT,
       -> PlayerName varchar(20),
       -> PlayerScore int,
       -> PRIMARY KEY(Id)
       -> );
    Query OK, 0 rows affected (0.58 sec)
    25 trả về
    mysql> create table selectMultipleSumDemo
       -> (
       -> Id int NOT NULL AUTO_INCREMENT,
       -> PlayerName varchar(20),
       -> PlayerScore int,
       -> PRIMARY KEY(Id)
       -> );
    Query OK, 0 rows affected (0.58 sec)
    93, không phải số không
  • Tùy chọn
    mysql> create table selectMultipleSumDemo
       -> (
       -> Id int NOT NULL AUTO_INCREMENT,
       -> PlayerName varchar(20),
       -> PlayerScore int,
       -> PRIMARY KEY(Id)
       -> );
    Query OK, 0 rows affected (0.58 sec)
    94 hướng dẫn hàm
    mysql> create table selectMultipleSumDemo
       -> (
       -> Id int NOT NULL AUTO_INCREMENT,
       -> PlayerName varchar(20),
       -> PlayerScore int,
       -> PRIMARY KEY(Id)
       -> );
    Query OK, 0 rows affected (0.58 sec)
    25 tính tổng của chỉ các giá trị riêng biệt trong một tập hợp
  • Hàm
    mysql> create table selectMultipleSumDemo
       -> (
       -> Id int NOT NULL AUTO_INCREMENT,
       -> PlayerName varchar(20),
       -> PlayerScore int,
       -> PRIMARY KEY(Id)
       -> );
    Query OK, 0 rows affected (0.58 sec)
    25 bỏ qua các giá trị
    mysql> create table selectMultipleSumDemo
       -> (
       -> Id int NOT NULL AUTO_INCREMENT,
       -> PlayerName varchar(20),
       -> PlayerScore int,
       -> PRIMARY KEY(Id)
       -> );
    Query OK, 0 rows affected (0.58 sec)
    93 trong phép tính

Minh họa hàm MySQL mysql> create table selectMultipleSumDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> PlayerName varchar(20),    -> PlayerScore int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.58 sec)25

Đầu tiên, tạo một bảng mới tên là

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
99

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
2

Sau đó, chèn một số hàng vào bảng

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
99

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
9

Thứ ba, sử dụng hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 để tính tổng giá trị trong cột
mysql> select *from selectMultipleSumDemo;
22

mysql> select *from selectMultipleSumDemo;
2
Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

Như bạn có thể thấy, hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 tính tổng của 1, 1, 2 và 3. Và nó bỏ qua NULL

Cuối cùng, sử dụng tùy chọn

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 với tùy chọn
mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
94 để tính tổng giá trị trong cột
mysql> select *from selectMultipleSumDemo;
22

mysql> select *from selectMultipleSumDemo;
8
Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

Trong trường hợp này, tùy chọn

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 với tùy chọn
mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
94 chỉ tính tổng các giá trị riêng biệt là 1, 2 và 3.

Ví dụ về chức năng MySQL mysql> create table selectMultipleSumDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> PlayerName varchar(20),    -> PlayerScore int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.58 sec)25

Hãy xem bảng

mysql> select *from selectMultipleSumDemo;
80 trong cơ sở dữ liệu mẫu

Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

1) Ví dụ hàm MySQL mysql> create table selectMultipleSumDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> PlayerName varchar(20),    -> PlayerScore int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.58 sec)25 đơn giản

Ví dụ này sử dụng hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 để lấy tổng số mặt hàng của chi tiết đơn hàng

+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
5
Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

2) Ví dụ về hàm MySQL mysql> create table selectMultipleSumDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> PlayerName varchar(20),    -> PlayerScore int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.58 sec)25 với biểu thức

Phần sau hiển thị các mục hàng đặt hàng của số thứ tự 10110

+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
7
Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

Để tính tổng cho đơn hàng số 10110, bạn sử dụng hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 như sau.

+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
9
Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

Trong hướng dẫn này, hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 tính tổng biểu thức sau của tất cả các mục hàng có số thứ tự 10110.

mysql> select
   -> SUM(CASE WHEN PlayerName='Maxwell' THEN PlayerScore END) AS 'MAXWELL TOTAL SCORE',
   -> SUM(CASE WHEN PlayerName='Ricky' THEN PlayerScore END) AS 'RICKY TOTAL SCORE',
   -> SUM(CASE WHEN PlayerName='David' THEN PlayerScore END) AS 'DAVID TOTAL SCORE'
   -> from selectMultipleSumDemo;
1

3) MySQL mysql> create table selectMultipleSumDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> PlayerName varchar(20),    -> PlayerScore int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.58 sec)25 với ví dụ về mệnh đề mysql> select *from selectMultipleSumDemo;87

Hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 thường dùng với mệnh đề
mysql> select *from selectMultipleSumDemo;
89 để tính tổng cho từng nhóm

Ví dụ: bạn có thể tính toán tổng số tiền của từng đơn đặt hàng bằng cách sử dụng hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 với mệnh đề
mysql> select *from selectMultipleSumDemo;
87 như được hiển thị trong truy vấn sau

mysql> select
   -> SUM(CASE WHEN PlayerName='Maxwell' THEN PlayerScore END) AS 'MAXWELL TOTAL SCORE',
   -> SUM(CASE WHEN PlayerName='Ricky' THEN PlayerScore END) AS 'RICKY TOTAL SCORE',
   -> SUM(CASE WHEN PlayerName='David' THEN PlayerScore END) AS 'DAVID TOTAL SCORE'
   -> from selectMultipleSumDemo;
8

Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

trong ví dụ này

  • Mệnh đề
    mysql> select *from selectMultipleSumDemo;
    87 chia chi tiết đơn đặt hàng thành các nhóm được nhóm theo số thứ tự
  • Hàm
    mysql> create table selectMultipleSumDemo
       -> (
       -> Id int NOT NULL AUTO_INCREMENT,
       -> PlayerName varchar(20),
       -> PlayerScore int,
       -> PRIMARY KEY(Id)
       -> );
    Query OK, 0 rows affected (0.58 sec)
    25 tính tổng từng số tiền của từng đơn hàng

4) Ví dụ về MySQL mysql> create table selectMultipleSumDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> PlayerName varchar(20),    -> PlayerScore int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.58 sec)25 với mệnh đề +----+------------+-------------+ | Id | PlayerName | PlayerScore | +----+------------+-------------+ |  1 | Maxwell    |          89 | |  2 | Ricky      |          98 | |  3 | Maxwell    |          96 | |  4 | Ricky      |          78 | |  5 | Maxwell    |          51 | |  6 | Ricky      |          89 | |  7 | David      |          56 | |  8 | David      |          65 | +----+------------+-------------+ 8 rows in set (0.00 sec)55

Bạn có thể sử dụng hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 trong mệnh đề
+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
57 để lọc nhóm. Ví dụ này minh họa cách chọn các đơn đặt hàng có số tiền đặt hàng lớn hơn
+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
58

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
20
Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

5) MySQL mysql> create table selectMultipleSumDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> PlayerName varchar(20),    -> PlayerScore int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.58 sec)25 với ví dụ về mysql> create table selectMultipleSumDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> PlayerName varchar(20),    -> PlayerScore int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.58 sec)93

Hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 trả về
mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
93 nếu tập kết quả trống. Đôi khi, bạn có thể muốn hàm
mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 trả về 0 thay vì
mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
93

Trong trường hợp này, bạn có thể sử dụng hàm

+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
75. Hàm
+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
76 chấp nhận hai đối số và trả về đối số thứ hai nếu đối số đầu tiên là
mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
93;

Xem truy vấn sau

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
21
Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

6) MySQL mysql> create table selectMultipleSumDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> PlayerName varchar(20),    -> PlayerScore int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.58 sec)25 với ví dụ tham gia

Xem các bảng

+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
79 và
mysql> select *from selectMultipleSumDemo;
80 sau đây

Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

Bạn có thể sử dụng hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 trong mệnh đề
+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
92 với
+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
93 để tính tổng các giá trị trong một bảng dựa trên một điều kiện được chỉ định bởi các giá trị trong một bảng khác

Câu lệnh này sử dụng hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 để tính tổng số tiền của các đơn hàng bị hủy

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
22

7) Ví dụ MySQL SUM IF

Câu lệnh sau sử dụng hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 để tính số lượng hàng đã bán cho mỗi trạng thái đơn hàng

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
23
Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

Nếu bạn muốn xoay hàng thành cột, bạn có thể sử dụng hàm

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 với biểu thức
+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
97. Đó là loại logic
+----+------------+-------------+
| Id | PlayerName | PlayerScore |
+----+------------+-------------+
|  1 | Maxwell    |          89 |
|  2 | Ricky      |          98 |
|  3 | Maxwell    |          96 |
|  4 | Ricky      |          78 |
|  5 | Maxwell    |          51 |
|  6 | Ricky      |          89 |
|  7 | David      |          56 |
|  8 | David      |          65 |
+----+------------+-------------+
8 rows in set (0.00 sec)
98.

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
24
Làm cách nào để tính tổng nhiều cột trong MySQL?
Làm cách nào để tính tổng nhiều cột trong MySQL?

Trong hướng dẫn này, bạn đã học cách sử dụng hàm MySQL

mysql> create table selectMultipleSumDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> PlayerName varchar(20),
   -> PlayerScore int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.58 sec)
25 để tính tổng của một tập hợp các giá trị.

Làm cách nào để tính tổng giá trị nhiều cột trong MySQL?

Hàm MySQL SUM() truy xuất giá trị tổng của một biểu thức được tạo thành từ nhiều cột. Câu lệnh MySQL ở trên trả về tổng của phép nhân 'receive_qty' và 'purch_price' từ bảng mua hàng cho từng nhóm danh mục ('cate_id').

Làm cách nào để tính tổng các giá trị cột trong MySQL?

Hàm sum() của MySQL được sử dụng để trả về tổng giá trị tổng của một biểu thức. Nó trả về NULL nếu tập kết quả không có hàng nào. Nó là một trong những loại hàm tổng hợp trong MySQL. .
CHỌN TỔNG (biểu_thức tổng hợp)
TỪ bảng
[điều kiện NƠI];

Làm cách nào để tính toán hai cột trong MySQL?

Tất cả những gì bạn cần làm là sử dụng toán tử nhân (*) giữa hai cột nhân ( giá * số lượng ) trong một truy vấn CHỌN đơn giản.

Bạn có thể tính tổng trên các cột trong SQL không?

Có, bạn có thể TỔNG các giá trị DISTINCT . Nếu bạn muốn TỔNG một cột dựa trên các giá trị riêng biệt của một cột khác, bạn có thể muốn sử dụng truy vấn con để lấy các giá trị riêng biệt, rồi thực hiện một phép tính TỔNG. Xem phần Ví dụ để biết ví dụ về cách sử dụng SUM với các giá trị riêng biệt.