mysql按照时间分组查询的语句

数据库教程 2021-11-29 12:54:45 16

导读

mysql按年、月、周、日分组查询 1.按照年份分组查询 SELECTDATE_FORMAT(t.bill_time,'%Y')month_time,sum(t.pay_price)totalFROMf_billtGROUPBYmonth_time; 查询结果 2.按照月份分组查询 SELECTDATE_FORMAT(t.bill_time,'%Y-%m')month_time,sum(t.pay_price)totalFROM……

mysql 按年、月、周、日分组查询

1.按照年份分组查询

SELECT DATE_FORMAT(t.bill_time,'%Y') month_time,sum(t.pay_price) total FROM f_bill t GROUP BY month_time;

查询结果

mysql按照时间分组查询的语句

2.按照月份分组查询

SELECT DATE_FORMAT(t.bill_time,'%Y-%m') month_time,sum(t.pay_price) total FROM f_bill t GROUP BY month_time;

查询结果

mysql按照时间分组查询的语句

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;

查询结果

mysql按照时间分组查询的语句

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;

查询结果

mysql按照时间分组查询的语句

1253067 TFnetwork_cn