Mysql統計每年每個月的資料——詳細教程
阿新 • • 發佈:2019-12-23
Mysql統計每年每個月的資料(前端頁面統計圖實現)
最終想實現的效果圖,在這裡就不多廢話了,直接上效果圖,由於測試資料有幾個月是為0的,所以資料圖看著會有點怪怪。
接下來是資料庫的兩個表,這裡直接給你們程式碼了,你們根據自己的需求更改即可
1 -- 會員充值表 2 CREATE TABLE rechargeinfo( 3 r_num INT PRIMARY KEY COMMENT '充值編號', 4 v_card VARCHAR(12) COMMENT '會員卡號', 5 r_recharge DOUBLE COMMENT '充值金額', 6 r_payway VARCHAR(20) COMMENT '支付方式', 7 o_id INT COMMENT '操作員工編號', 8 r_time DATETIME COMMENT '交易時間', 9 r_remark VARCHAR(50) COMMENT '交易備註', 10 FOREIGN KEY (o_id) REFERENCES operatorinfo(o_id) 11 )ENGINE = InnoDB COMMENT '會員充值資訊表'; 12 alter table rechargeinfo change r_time r_time timestamp not null default NOW(); 13 14 --停車登記表 15 16 CREATE TABLE parkinginfo( 17 p_num INT PRIMARY KEY COMMENT '停車編號', 18 c_carplate VARCHAR(20) NOT NULL COMMENT '車牌號', 19 p_card VARCHAR(20) COMMENT '停車牌號', 20 p_picture VARCHAR(50) COMMENT '進場拍攝圖', 21 p_entrytime Date COMMENT '進場時間', 22 p_leavetime Date COMMENT '出場時間', 23 p_type VARCHAR(10) COMMENT '客戶型別', 24 p_cost Double COMMENT '停車費用', 25 p_payway VARCHAR(20) COMMENT '支付方式', 26 v_card VARCHAR(12) COMMENT '會員卡號', 27 v_phone VARCHAR(12) COMMENT '臨時客戶手機號碼', 28 p_condition VARCHAR(20) DEFAULT '正在停車中' COMMENT '狀態', 29 p_remark VARCHAR(50) COMMENT '備註' 30 31 )ENGINE = InnoDB COMMENT '停車資訊表'; 32 alter table parkinginfo change p_entrytime p_entrytime timestamp not null default NOW();
接下來就是重點了:
SQL語句,只需要傳入一個引數(年份)即可, 這個是統計會員充值表的,另一張表同理
1 select 2 sum(case month(r_time) when '1' then r_recharge else 0 end) as Jan, 3 sum(case month(r_time) when '2' then r_recharge else 0 end) as Feb, 4 sum(case month(r_time) when '3' then r_recharge else 0 end) as Mar, 5 sum(case month(r_time) when '4' then r_recharge else 0 end) as Apr, 6 sum(case month(r_time) when '5' then r_recharge else 0 end) as May, 7 sum(case month(r_time) when '6' then r_recharge else 0 end) as June, 8 sum(case month(r_time) when '7' then r_recharge else 0 end) as July, 9 sum(case month(r_time) when '8' then r_recharge else 0 end) as Aug, 10 sum(case month(r_time) when '9' then r_recharge else 0 end) as Sept, 11 sum(case month(r_time) when '10' then r_recharge else 0 end) as Oct, 12 sum(case month(r_time) when '11' then r_recharge else 0 end) as Nov, 13 sum(case month(r_time) when '12' then r_recharge else 0 end) as Dece 14 from rechargeinfo 15 where year(r_time)='2019';
效果圖:可以看到,每個月的資料已經查出來了
接下來給出的是Dao層程式碼,service層就不寫了,是個程式設計師,dao層知道,service都會寫
import java.util.Map; public interface TotalDao { Map<String,Double> getRechargeTotal(String toyear); Map<String,Double> getParkingTotal(String toyear); }
以及Mapper檔案程式碼:注意,這裡的結果型別一定要是java.util.LinkedHashMap, 如果是HashMap,是不會報錯,但是順序會亂,So,,你懂得。
<!-- 統計充值--> <select id="getRechargeTotal" parameterType="String" resultType="java.util.LinkedHashMap"> select sum(case month(r_time) when '1' then r_recharge else 0 end) as Jan, sum(case month(r_time) when '2' then r_recharge else 0 end) as Feb, sum(case month(r_time) when '3' then r_recharge else 0 end) as Mar, sum(case month(r_time) when '4' then r_recharge else 0 end) as Apr, sum(case month(r_time) when '5' then r_recharge else 0 end) as May, sum(case month(r_time) when '6' then r_recharge else 0 end) as June, sum(case month(r_time) when '7' then r_recharge else 0 end) as July, sum(case month(r_time) when '8' then r_recharge else 0 end) as Aug, sum(case month(r_time) when '9' then r_recharge else 0 end) as Sept, sum(case month(r_time) when '10' then r_recharge else 0 end) as Oct, sum(case month(r_time) when '11' then r_recharge else 0 end) as Nov, sum(case month(r_time) when '12' then r_recharge else 0 end) as Dece from rechargeinfo where year(r_time)=#{toyear}; </select> <!--統計停車--> <select id="getParkingTotal" parameterType="String" resultType="java.util.LinkedHashMap"> select sum(case month(p_leavetime) when '1' then p_cost else 0 end) as Jan, sum(case month(p_leavetime) when '2' then p_cost else 0 end) as Feb, sum(case month(p_leavetime) when '3' then p_cost else 0 end) as Mar, sum(case month(p_leavetime) when '4' then p_cost else 0 end) as Apr, sum(case month(p_leavetime) when '5' then p_cost else 0 end) as May, sum(case month(p_leavetime) when '6' then p_cost else 0 end) as June, sum(case month(p_leavetime) when '7' then p_cost else 0 end) as July, sum(case month(p_leavetime) when '8' then p_cost else 0 end) as Aug, sum(case month(p_leavetime) when '9' then p_cost else 0 end) as Sept, sum(case month(p_leavetime) when '10' then p_cost else 0 end) as Oct, sum(case month(p_leavetime) when '11' then p_cost else 0 end) as Nov, sum(case month(p_leavetime) when '12' then p_cost else 0 end) as Dece from parkinginfo where year(p_leavetime)=#{toyear} and p_condition='交易完成' ; </select> </mapper>
Control層:
1 //統計頁面 2 @RequestMapping("/totalui") 3 public ModelAndView test(@RequestParam(value ="toyear",required = false,defaultValue = "2019")String toyear){ 4 ModelAndView mv = new ModelAndView(); 5 // get all data 6 Map<String, Double> rechargeTotal = service.getRechargeTotal(toyear); 7 Map<String,Double> pachargeTotal = service.getParkingTotal(toyear); 8 // test 9 System.out.println("測試所有資料: "+rechargeTotal.values()+" "); 10 System.out.println("測試所有資料 : "+ pachargeTotal.values()+" "); 11 12 // 統計 13 Map<String,Double> datatotal = new LinkedHashMap<>(); 14 for(String key:rechargeTotal.keySet()){ 15 if(pachargeTotal.containsKey(key)){ 16 datatotal.put(key, rechargeTotal.get(key)+pachargeTotal.get(key)); 17 } 18 } 19 System.out.println("合併後的資料!!!"+datatotal.values()); 20 21 // set atrr 22 mv.addObject("redata",rechargeTotal.values()); 23 mv.addObject("padata",pachargeTotal.values()); 24 mv.addObject("totaldata",datatotal.values()); 25 mv.setViewName("Income"); 26 return mv; 27 28 }
前端頁面顯示圖程式碼:
1 <script> 2 var chart = Highcharts.chart('container', { 3 chart: { 4 type: 'column' 5 }, 6 title: { 7 text: '洱海灣停車場營業額統計' 8 }, 9 10 legend: { 11 align: 'right', 12 verticalAlign: 'middle', 13 layout: 'vertical' 14 }, 15 xAxis: { 16 categories: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'], 17 labels: { 18 x: -10 19 } 20 }, 21 yAxis: { 22 allowDecimals: false, 23 title: { 24 text: '金額' 25 } 26 }, 27 series: [{ 28 name: '會員充值收入', 29 data: ${redata} 30 }, { 31 name: '停車收入', 32 data: ${padata} 33 }, { 34 name: '合計收入', 35 data: ${totaldata} 36 }], 37 responsive: { 38 rules: [{ 39 condition: { 40 maxWidth: 1500 41 }, 42 chartOptions: { 43 legend: { 44 align: 'center', 45 verticalAlign: 'bottom', 46 layout: 'horizontal' 47 }, 48 yAxis: { 49 labels: { 50 align: 'left', 51 x: 0, 52 y: -5 53 }, 54 title: { 55 text: '¥金額以元為單位' 56 } 57 }, 58 subtitle: { 59 text: null 60 }, 61 credits: { 62 enabled: false 63 } 64 } 65 }] 66 } 67 }); 68 69 </script>
大功告成,不懂得可以直接評論諮詢!!!