时间:2021-12-23 10:20:39 | 栏目:Mysql | 点击:次
mysql 按年、月、周、日分组查询
1.按照年份分组查询
SELECT DATE_FORMAT(t.bill_time,'%Y') month_time,sum(t.pay_price) total FROM f_bill t GROUP BY month_time;
查询结果
2.按照月份分组查询
SELECT DATE_FORMAT(t.bill_time,'%Y-%m') month_time,sum(t.pay_price) total FROM f_bill t GROUP BY month_time;
查询结果
3.按照周分组查询
SELECT CONCAT(SUBSTR(DATE_FORMAT(t.bill_time,'%Y-%u') FROM 1 FOR 4),'第',SUBSTR(DATE_FORMAT(t.bill_time,'%Y-%u'),6),'周') week_time,sum(t.price) total FROM f_bill t GROUP BY week_time;
查询结果
4.按照日分组查询
SELECT DATE_FORMAT(t.bill_time,'%Y-%m-%d') month_time,sum(t.pay_price) total FROM f_bill t GROUP BY month_time;
查询结果