MySQL中如何按月统计数据
2016-03-09 09:52
579 查看
表finance有俩个字段如下
date date
money double(15,2)
下面需要对表 finance的2010年财务数据,按月进行统计
Sql代码
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代码
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
相关文章推荐
- mysql隔离级别的设置和检索
- MYSQL锁表问题的解决方法
- Windows下Mysql错误ERROR 1045 (28000) ERROR 1054 (42S22)以及ERROR 1820 (HY000)的解决
- Mysql主从环境搭建
- MySql视频教程——百度云下载路径
- Spark读取数据库(Mysql)的四种方式讲解
- MySQL where 查询不能使用别名的解决方法
- [R] How to install RMySQL package on Window
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
- MYSQL锁表问题的解决方法
- MyBatis批量插入数据Mysql
- mysql ip 存储
- mysql报1067错
- MySQL InnoDB 行锁实现
- Ubuntu安装配置Mysql
- 浅谈MySql的存储引擎(表类型)
- 13个mysql数据库的实用SQL小技巧
- 详解mysql int类型的长度值问题
- MySQL本地可以连接,远程连接不上的解决
- MYSQL添加新用户 MYSQL为用户创建数据库 MYSQL为新用户分配权限