Để 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)
ghi bàn. 1 câu trả lời được chấp nhận sử dụng 3 để kết hợp 4 và 5 trong một cột, sau đó thực hiện 6mysql> 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)25Hà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
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 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 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)25Hãy xem bảng mysql> select *from selectMultipleSumDemo;80 trong cơ sở dữ liệu mẫu 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ảnVí 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 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ứcPhầ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 Để 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 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;87Hà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 trong ví dụ này
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)55Bạ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 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)93Hà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 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 giaXem 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 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 IFCâ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 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 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. |