Last time in the blog http://www.cnblogs.com/Mr-Cxy/p/5923375.html
We used the group BY with Rollup function field custom sort to implement adding the bottom total, the code is very long,
And then we use the vertical UNION ALL function Ifnull sum (if ()) function to achieve vertical merge add total words
SELECTIfnull (F. City, "Total") asCity, f.7 month amount, f.8 month amount, f.9 month amount from ( SELECTE.* from ( SELECTB. Cities,SUM(IF(b. Month and year=201607, B. Amount,NULL)) asJuly Amount,SUM(IF(b. Month and year=201608, B. Amount,NULL)) asAugust Amount,SUM(IF(b. Month and year=201609, B. Amount,NULL)) asSeptember Amount from ( SELECTCity asCity, Date_format (Order_time, "%Y%M ") asYearsSUM(Pay_money) asAmount fromTest_a03order asaGROUP byCity,date_format (Order_time, "%Y%m ")) asbGROUP byB. City) aseUNION All( SELECT SUM(IF(1=2,0,NULL)) asCitySUM(IF(d. Month=201607, D. Amount,NULL)) asJuly Amount,SUM(IF(d. Month=201608, D. Amount,NULL)) asAugust Amount,SUM(IF(d. Month=201609, D. Amount,NULL)) asSeptember Amount from ( SELECTCity asCity, Date_format (Order_time, "%Y%M ") asYearsSUM(Pay_money) asAmount fromTest_a03order asCGROUP byCity,date_format (Order_time, "%Y%m ")) asd)) asF
MySQL data at the bottom of the total words the second method of vertical merger 20161103