MySQL 生成各种时间点,如上月第一天最后一天,上周第一天最后一天等

发布时间:2015年05月11日 // 分类:代码 // 暂无评论

MySQL中生成各种时间节点,方便报表生成。在需要处使用 where 字段 between @YT_S and @YT_E 即可

set @now=curdate();
-- 批量生成日期
-- 年度 本年开始 本年结束 上年开始 上年结束
set @YT_S=date_sub(date_sub(@now,interval 0 year),interval dayofyear(@now)-1 day);
set @YT_E=date_sub(date_sub(@now,interval -1 year),interval dayofyear(@now) day);
set @YL_S=date_sub(date_sub(@now,interval 1 year),interval dayofyear(@now)-1 day);
set @YL_E=date_sub(date_sub(@now,interval 0 year),interval dayofyear(@now) day);
-- 月份 本月开始 本月结束 上月开始 上月结束
set @MT_S=date_sub(date_sub(@now,interval 0 month),interval dayofmonth(@now)-1 day);
set @MT_E=date_sub(date_sub(@now,interval -1 month),interval dayofmonth(@now) day);
set @ML_S=date_sub(date_sub(@now,interval 1 month),interval dayofmonth(@now)-1 day);
set @ML_E=date_sub(date_sub(@now,interval 0 month),interval dayofmonth(@now) day);
-- 星期 本周开始 本周结束 上周开始 上周结束 // week较为不同
set @WT_S=date_sub(date_sub(@now,interval 0 week),interval weekday(@now) day);
set @WT_E=date_sub(date_sub(@now,interval -1 week),interval weekday(@now)+1 day);
set @WL_S=date_sub(date_sub(@now,interval 1 week),interval weekday(@now) day);
set @WL_E=date_sub(date_sub(@now,interval 0 week),interval weekday(@now)+1 day);
-- 日期 今天开始结束 昨天开始结束 前天开始结束
set @DT_S=@now;
set @DT_E=@now;
set @DY_S=date_sub(@now,interval 1 day);
set @DY_E=date_sub(@now,interval 1 day);
set @DB_S=date_sub(@now,interval 2 day);
set @DB_E=date_sub(@now,interval 2 day);
-- 批量将日期转为时间戳
set @YT_S=date_format(@YT_S,'%Y-%m-%d 00:00:00');
set @YL_S=date_format(@YL_S,'%Y-%m-%d 00:00:00');
set @MT_S=date_format(@MT_S,'%Y-%m-%d 00:00:00');
set @ML_S=date_format(@ML_S,'%Y-%m-%d 00:00:00');
set @WT_S=date_format(@WT_S,'%Y-%m-%d 00:00:00');
set @WL_S=date_format(@WL_S,'%Y-%m-%d 00:00:00');
set @DT_S=date_format(@DT_S,'%Y-%m-%d 00:00:00');
set @DY_S=date_format(@DY_S,'%Y-%m-%d 00:00:00');
set @DB_S=date_format(@DB_S,'%Y-%m-%d 00:00:00');
set @YT_E=date_format(@YT_E,'%Y-%m-%d 23:59:59');
set @YL_E=date_format(@YL_E,'%Y-%m-%d 23:59:59');
set @MT_E=date_format(@MT_E,'%Y-%m-%d 23:59:59');
set @ML_E=date_format(@ML_E,'%Y-%m-%d 23:59:59');
set @WT_E=date_format(@WT_E,'%Y-%m-%d 23:59:59');
set @WL_E=date_format(@WL_E,'%Y-%m-%d 23:59:59');
set @DT_E=date_format(@DT_E,'%Y-%m-%d 23:59:59');
set @DY_E=date_format(@DY_E,'%Y-%m-%d 23:59:59');
set @DB_E=date_format(@DB_E,'%Y-%m-%d 23:59:59');

本文固定链接
https://www.ywlib.com/archives/10.html

标签
mysql

添加新评论 »

分类
随机文章
最新文章
最近回复