To create a test table:
DROP TABLE IF EXISTS ' test ';
CREATE TABLE ' test ' (
' year ' int ') default null,
' month ' int (one) default null,
' amount ' double default null< c5/>) Engine=innodb DEFAULT Charset=utf8;
Insert data:
INSERT into ' Test ' VALUES (' 1991 ', ' 1 ', ' 1.1 ');
INSERT into ' Test ' VALUES (' 1991 ', ' 2 ', ' 1.2 ');
INSERT into ' Test ' VALUES (' 1991 ', ' 3 ', ' 1.3 ');
INSERT into ' Test ' VALUES (' 1991 ', ' 4 ', ' 1.4 ');
INSERT into ' Test ' VALUES (' 1992 ', ' 1 ', ' 2.1 ');
INSERT into ' Test ' VALUES (' 1992 ', ' 2 ', ' 2.2 ');
INSERT into ' Test ' VALUES (' 1992 ', ' 3 ', ' 2.3 ');
INSERT into ' Test ' VALUES (' 1992 ', ' 4 ', ' 2.3 ');
See the question request, think about it can:
Generates a column + with ROLLUP using sum (IF ()) to generate a total row and ifnull the Total row header as Total_num
Realize
The SQL code block is as follows:
Select year,
sum (if (month=1,amount,0)) as "M1",
sum (if (month=2,amount,0) as "M2",
sum (if (month=3, amount,0) as "M3",
sum (if (month=4,amount,0)) as "M4" from
Test GROUP by year
;
The effect is as follows:
The above is a small series to introduce the MySQL column to modify the row and show the simple realization of data, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!