电竞比分网-中国电竞赛事及体育赛事平台

分享

MySQL的經(jīng)典用法(二)----按月統(tǒng)計數(shù)據(jù)

 kekokeko 2011-01-14

MySQL的經(jīng)典用法(二)----按月統(tǒng)計數(shù)據(jù)

文章分類:數(shù)據(jù)庫
    表finance有倆個字段如下
date date
money double(15,2)
    下面需要對表finance的2010年財務數(shù)據(jù),按月進行統(tǒng)計

Sql代碼 復制代碼
  1. select DATE_FORMAT(date,'%Y-%m'as month,sum(money) as money    
  2. from finance    
  3. where DATE_FORMAT(date,'%Y')=2010    
  4. group by month    
  5. order by month  

    下面是按周統(tǒng)計
查看MySQL的manual
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
    其中
1如果周一為一周的第一天,則(小寫) DATE_FORMAT(date,'%x %v')
2如果周日為一周的第一天,則(大寫) DATE_FORMAT(date,'%X %V')
Sql代碼 復制代碼
  1. select DATE_FORMAT(date,'%x年-第%v周'as week,sum(money) as money    
  2. from finance_base    
  3. where DATE_FORMAT(date,'%Y')=2010    
  4. group by week  

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多