sql半小时,小时,天,月,年分组
每半小时
FLOOR( MINUTE ( stime ) / 30 ) * 30 )是按照 30 分钟分组
FLOOR( MINUTE ( stime ) / 10 ) * 10 )是按照 10 分钟分组
//每半小时
SELECT TIME
FROM
(
SELECT
stime,DATE_FORMAT(
CONCAT( DATE( stime ), ' ', HOUR ( stime ), ':', FLOOR( MINUTE ( stime ) / 30 ) * 30 ),
'%Y-%m-%d %H:%i'
) AS TIME
FROM chatting
) a
GROUP BY DATE_FORMAT( TIME, '%Y-%m-%d %H:%i' )
每小时
也可以这样 修改模板回去不同的分组时间
‘%Y-%m-%d %H:00:00’ 每小时
‘%Y-%m-%d 00:00:00’ 每天
//每小时
SELECT DATE_FORMAT(stime, '%Y-%m-%d %H:00:00') AS TIME
FROM chatting
GROUP BY TIME
每天
用这个也可以,上面的那种方法也行
//每天
SELECT DATE(stime) AS TIME
FROM chatting
GROUP BY TIME
日期函数
select day(createtime) from life_unite_product --取时间字段的天值
select month(createtime) from life_unite_product --取时间字段的月值
select year(createtime) from life_unite_product --取时间字段的年值
两时间的差
select DATEDIFF(yy,createtime,getdate()) from life_unite_product --与当前日期的年份差
select DATEDIFF(mm,createtime,getdate()) from life_unite_product --与当前日期的月份差
select DATEDIFF(dd,createtime,getdate()) from life_unite_product --与当前日期的日数差
select DATEDIFF(mi,createtime,getdate()) from life_unite_product --与当前日期的分钟数差
上面的都是mysql
sql server
select datepart(yy,createtime) from life_unite_product --取时间字段的年值
select datepart(qq,createtime) from life_unite_product --取时间字段的季度值
select datepart(mm,createtime) from life_unite_product --取时间字段的月值
select datepart(dy,createtime) from life_unite_product --取时间字段是那年的第几天
select datepart(dd,createtime) from life_unite_product --取时间字段的天值
select datepart(wk,createtime) from life_unite_product --取时间字段是那年的第几个星期
select datepart(dw,createtime) from life_unite_product --取时间字段是那年的那个星期的第几个工作日(工作日从星期日开算)
select datepart(hh,createtime) from life_unite_product --取时间字段的小时值
select datepart(mi,createtime) from life_unite_product --取时间字段的分钟值
select datepart(ss,createtime) from life_unite_product --取时间字段的秒值
select datepart(ms,createtime) from life_unite_product --取时间字段的毫秒值
select dateadd(yy,-1,createtime) from life_unite_product ----取时间字段(年份被减1了)
select dateadd(mm,3,createtime) from life_unite_product ----取时间字段(月份被加3了)
select dateadd(dd,1,createtime) from life_unite_product ----取时间字段(日被加1了)
select datename(yy,createtime) from life_unite_product --取时间字段的年值
select datename(mm,createtime) from life_unite_product --取时间字段的月值
select datename(dd,createtime) from life_unite_product --取时间字段的天值