MySql 数据库统计中必用的函数

MySql 数据库统计中必用的函数

 

 

 

date_format:时间格式化函数

<span">举例:按照年份统计

select count(*) counts,date_format(OffsetTime,'%Y') year from mytable group by date_format(col, '%Y');

按照月份统计

 from mytable group by date_format(col, '%Y-%m');

按照季度统计

from mytable group by concat(date_format(col, '%Y'),FLOOR((date_format(col, '%m')+2)/3)); 

to_days :转换成天;

N天内记录

WHERE TO_DAYS(NOW())-TO_DAYS(时间字段)<=N

quarter:季度函数

SELECT id, quarter(my_time) FROM mytable;

curdate():当前日期

year():  当前年份 : month():当前月份;week():当前星期

--当月查询:
where month(coltime) = month(curdate()) and year(coltime) = year(curdate())

--当周查询
where year(my_time1) = year(curdate()) and week(my_time2) = week(curdate())

 

留言