Mysql columns Modify rows and display the simple implementation of the data _mysql

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.