加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL中如何按月统计数据

发布时间:2016-09-28 11:22:17 所属栏目:MySql教程 来源:站长网
导读:表finance有俩个字段如下 date date money double(15,2) 下面需要对表 finance的2010年财务数据,按月进行统计 Sql代码 select DATE_FORMAT(date,'%Y-%m') as month,sum(money) as money from finance where DATE_FORMAT(date,'%Y')=2010 group by month o

表finance有俩个字段如下

date date

money double(15,2)

下面需要对表 finance的2010年财务数据,按月进行统计

Sql代码

select DATE_FORMAT(date,'%Y-%m') as month,sum(money) as money   
from finance   
where DATE_FORMAT(date,'%Y')=2010   
group by month   
order by month

URL:http://www.bianceng.cn/database/MySQL/201410/46063.htm

下面是按周统计

查看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代码

select DATE_FORMAT(date,'%x年-第%v周') as week,sum(money) as money   
from finance_base   
where DATE_FORMAT(date,'%Y')=2010   
group by week

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读