Dưới đây là dữ liệu của bạn được nạp
mysql> use junk
Database changed
mysql> drop table if exists summation_trick;
Query OK, 0 rows affected (0.04 sec)
mysql> create table summation_trick
-> (
-> col1 varchar(20),
-> col2 int
->);
Query OK, 0 rows affected (0.09 sec)
mysql> insert into summation_trick values
-> ('Water',22 ),('water', 3 ),
-> ('water', 5 ),('Air' ,10 ),
-> ('Earth', 3 ),('Air' , 5 );
Query OK, 6 rows affected (0.05 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql>
và đây là truy vấn thực hiện
mysql> select IFNULL(col1,'SUM') col1,sumcol2
-> from (select col1,col2,sum(col2) sumcol2
-> from summation_trick
-> group by col1,col2
-> with rollup) B
-> where (col1 is null and col2 is null)
-> or (col1 is not null and col2 is not null);
+-------+---------+
| col1 | sumcol2 |
+-------+---------+
| Air | 5 |
| Air | 10 |
| Earth | 3 |
| water | 3 |
| water | 5 |
| Water | 22 |
| SUM | 48 |
+-------+---------+
7 rows in set (0.00 sec)
mysql>
tôi thấy những gì bạn đang nói về bạn không biết lý do tại sao tôi muốn sử dụng nó tuy nhiên điều này công trình, như tôi đã nói những gì im tìm kiếm là để hav tổng col2 ở dưới cùng của cột – dames