With rollup usage in MySQL

Source: Internet
Author: User

1.WITH ROLLUP: Statistical data is performed on the basis of the Group field.

Example: First group on the Name field, then some field statistics on the basis of the grouping, the table structure is as follows:

CREATE TABLE' Test ' (' Id ')int( One) not NULLauto_increment, ' title 'varchar( -)DEFAULT NULLCOMMENT'title', ' uid 'int( One)DEFAULT NULLCOMMENT'UID',  ` Money`decimal(2,0)DEFAULT '0', ' name 'varchar( -)DEFAULT NULL,  PRIMARY KEY(' Id ')) ENGINE=InnoDB auto_increment=8 DEFAULTCHARSET=UTF8MB4;

Save a few data to see:

INSERT  into' Test '. ' Test ' (' Id ', ' title ', ' uid ', ' Money', ' name ')VALUES('2','National Day','2',' A','Botong Tong');INSERT  into' Test '. ' Test ' (' Id ', ' title ', ' uid ', ' Money', ' name ')VALUES('3','This is a 8-day holiday .','3',' -','old Urchin .');INSERT  into' Test '. ' Test ' (' Id ', ' title ', ' uid ', ' Money', ' name ')VALUES('4','This is Uid=1 's first piece of data.','1',' -','Ouyang Fung');INSERT  into' Test '. ' Test ' (' Id ', ' title ', ' uid ', ' Money', ' name ')VALUES('5','The Little Lord of the white spirit','4',' About','Ouyang');INSERT  into' Test '. ' Test ' (' Id ', ' title ', ' uid ', ' Money', ' name ')VALUES('7','Nine founder of Yin Canon','3',' A','little Urchin .');INSERT  into' Test '. ' Test ' (' Id ', ' title ', ' uid ', ' Money', ' name ')VALUES('8','hands on each other's blog','2',' About','Botong Tong');INSERT  into' Test '. ' Test ' (' Id ', ' title ', ' uid ', ' Money', ' name ')VALUES('9','The palm of Ecstasy','2',' +','Botong Tong');INSERT  into' Test '. ' Test ' (' Id ', ' title ', ' uid ', ' Money', ' name ')VALUES('Ten','Toad Gong','1',' $','Ouyang Fung');INSERT  into' Test '. ' Test ' (' Id ', ' title ', ' uid ', ' Money', ' name ')VALUES(' One','The palm of Lore','3',' -','little Urchin .');INSERT  into' Test '. ' Test ' (' Id ', ' title ', ' uid ', ' Money', ' name ')VALUES(' A','Nine Yin Canon','3',' -','old Urchin .');

Group Statistics:

SELECT SUM (Money as money from   GROUP  by  with ROLLUP;

You can see the sum of money after grouping by name. Above see null 1242, how to make an individual name field such as Total amount: 1242? can also drip, we continue:

COALESCE (A,B,C); parameter description: If a==null, select B; If b==null, select C; If A!=null, select A; if a b c is null, the return is null (meaningless).
SELECT COALESCE ' Total Amount ' SUM (Money as money from   GROUP  by  with ROLLUP;

As can be seen in the data summary. Use is still very convenient drip.

With rollup usage in MySQL

Related Article

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.