mysql跨年按周分组_MySQL
程序员文章站
2022-05-17 11:53:34
...
bitsCN.com
mysql跨年按周分组
Sql代码 use employees; create table sales( id int auto_increment, date datetime not null, cost int unsigned not null, primary key(id) ); insert into sales (date,cost)values('2010-12-31',100); insert into sales (date,cost)values('2011-01-01',200); insert into sales (date,cost)values('2011-01-02',100); insert into sales (date,cost)values('2011-01-06',100); insert into sales (date,cost)values('2011-01-10',100); SELECT * FROM employees.sales; select week(date),sum(cost)from sales group by week(date); select floor(datediff(date,'1900-01-01')/7) as a,sum(cost) from sales group by floor(datediff(date,'1900-01-01')/7); /*最终解决方案*/ select date_add('1900-01-01',interval floor(datediff(date,'1900-01-01')/7)*7 day) as week_start, date_add('1900-01-01',interval floor(datediff(date,'1900-01-01')/7)*7+6 day)as week_end,sum(cost) from sales group by floor(datediff(date,'1900-01-01')/7);
bitsCN.com
上一篇: php生成器使用方法
下一篇: PHP生成横状百分比图片实例
推荐阅读
-
tp框架如何处理mysql先排序在分组
-
MySQL按常规排序、自定义排序和按中文拼音字母排序的方法
-
MySQL数据库Group by分组之后再统计数目Count(*)与不分组直接统计数目的区别
-
解决大于5.7版本mysql的分组报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
-
详解 Mysql查询结果顺序按 in() 中ID 的顺序排列
-
快速回顾MySQL:汇总和分组
-
mysql分组函数及其用例
-
MySQL实现按天统计数据的方法
-
mysql查询、排序、分组
-
MySQL分组查询group by实例