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