mysql 时间戳转换 今天、昨天、7天、本月、上月

下面的时间范围查询都是前闭后开,如 $time_start <= time < $time_end

if $day_index == 1 then #今天数据

set $time_start = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 0 DAY),INTERVAL 0 HOUR));
set $time_end = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 0 DAY),INTERVAL 24 HOUR));

elseif $day_index == 2 then #昨天数据

set $time_start = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 1 DAY),INTERVAL 0 HOUR));
set $time_end = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 1 DAY),INTERVAL 24 HOUR));

elseif $day_index == 3 then #7天数据

set $time_start = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 6 DAY),INTERVAL 0 HOUR)); #6天前
set $time_end = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 0 DAY),INTERVAL 24 HOUR)); #今天结束

elseif $day_index == 4 then #本月数据

set $time_start = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(curdate(), INTERVAL 0 MONTH),INTERVAL 0 DAY)); #本月开始
set $time_end = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 0 DAY),INTERVAL 24 HOUR)); #今天结束

elseif $day_index == 5 then #上月数据

set $time_start = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(curdate(), INTERVAL 1 MONTH),INTERVAL 0 DAY)); #上月开始
set $time_end = UNIX_TIMESTAMP(DATE_ADD(DATE_SUB(curdate(), INTERVAL 0 MONTH),INTERVAL 0 DAY)); #本月开始

end if;

上一篇:EdgeX系列之五 -- 设置定时任务


下一篇:SQL SERVER 时间查询