mysql时间格式化到小时_mysql 时间格式化

格式化时间例子:

date_format(now(),'%y-%m-%d %H:%i:%S')

项目中的统计报表作的很多,需求中有按周、月统计数据的。查看了Mysql的API,发现Date_format是格式化日期的,看了Date_format()的具体说明后就用这个函数按周统计,sql大致如下:

select   DATE_FORMAT(check_date ,'%X-%V')  dates,avg(weight)/10000  weight from ho_body where user_id=295

and  weight >0 and check_date  between '2009-02-24' and '2010-02-24' group by dates

------------------------------------------------------------------------------------------------------

查出的结果为:

dates       weight

2009-30   80.9

2009-31   80.425

2009-32   80.76666667

2009-33   80.75384615

2009-34   80.8

2009-35   79.88

2009-36   80.06

2009-37   79.875

2009-38   79.075

2009-39   79.26666667

2009-40   79.16666667

2009-41   78.875

2009-42   78.33333333

2009-43   78.77272727

2009-44   77.625

2009-45   77.825

2009-46   77.575

2009-47   77.45

2009-48   81.25

2009-49   76.5

2009-50   77.83333333

2009-52   79.8

2010-02   79.2

检查了一下,大致没有问题,然后又开始寻找如何按月统计,然后就看Date_format的语法,

%m表示数值,%M表示名称,看后很兴奋的试一下:

select   distinct  DATE_FORMAT(check_date ,'%X-%m')  c1,avg(weight)/10000  wei from ho_body where user_id=295

and  weight >0 and check_date  between '

你可能感兴趣的:(mysql时间格式化到小时)