Mysql 去年、前年、上季度、上两个季度写法(只供笔记)

2018-01-23  本文已影响0人  卖蟑螂的小男孩

背景:

需要获取当前时间点之前的时间段,比如 去年、前年、上个月、上个季度等等,在这里进行处理的都是最早的开始时间到截止时间,比如:
1.当前时间为‘2018-10-23’,去年就应该是 ‘2017-01-01’~'2017-12-31'
2.上个季度,应该是‘2018-07-01’~‘2018-09-30’ ,如果当前是11月份 上个月的时间段应该是不变的。

贴一个函数先
CREATE  FUNCTION `util_to_str`(util varchar(255),number int) RETURNS varchar(255) CHARSET utf8 COLLATE utf8_unicode_ci
BEGIN  
DECLARE result VARCHAR(255) DEFAULT '';
DECLARE start_time VARCHAR(255) DEFAULT '';
DECLARE end_time VARCHAR(255) DEFAULT '';
IF(util = 'YEAR') 
THEN 
SET start_time= date_add(date_format(now(), '%y-01-01'),interval number YEAR);
SET end_time= date_add(date_format(now(), '%y-12-31'),interval -1 YEAR);

ELSEIF(util = 'QUARTER')
THEN
IF(QUARTER(NOW())=1) THEN 
SET start_time= date_add(date_sub(date_format(now(), '%y-01-%d'),
                         interval extract(day from now()) - 1 day),
                interval number QUARTER);
SET end_time= date_add(date_sub(date_format(now(), '%y-01-%d'),
                         interval extract(day from now()) day),
                interval 0 QUARTER); 
 
ELSEIF(QUARTER(NOW())=2) THEN 
SET start_time= date_add(date_sub(date_format(now(), '%y-04-%d'),
                         interval extract(day from now()) - 1 day),
                interval number QUARTER);
SET end_time= date_add(date_sub(date_format(now(), '%y-04-%d'),
                         interval extract(day from now()) day),
                interval 0 QUARTER); 

ELSEIF(QUARTER(NOW())=3) THEN 
SET start_time= date_add(date_sub(date_format(now(), '%y-07-%d'),
                         interval extract(day from now()) - 1 day),
                interval number QUARTER);
SET end_time= date_add(date_sub(date_format(now(), '%y-07-%d'),
                         interval extract(day from now()) day),
                interval 0 QUARTER);  

ELSEIF(QUARTER(NOW())=4) THEN 
SET start_time= date_add(date_sub(date_format(now(), '%y-10-%d'),
                         interval extract(day from now()) - 1 day),
                interval number QUARTER);
SET end_time= date_add(date_sub(date_format(now(), '%y-10-%d'),
                         interval extract(day from now()) day),
                interval 0 QUARTER);  

END IF;  


ELSEIF(util = 'MONTH')
THEN
SET start_time= date_add(date_sub(date_format(now(), '%y-%m-%d'),
                         interval extract(day from now()) - 1 day),
                interval number MONTH);
SET end_time= date_add(date_sub(date_format(now(), '%y-%m-%d'),
                         interval extract(day from now()) day),
                interval 0 MONTH); 

ELSEIF(util = 'WEEK')
THEN
SET start_time= date_add( DATE_SUB(date_format(now(), '%y-%m-%d'), INTERVAL WEEKDAY(date_format(now(), '%y-%m-%d')) DAY), INTERVAL number WEEK);
SET end_time= date_add(date_add( DATE_SUB(date_format(now(), '%y-%m-%d'), INTERVAL WEEKDAY(date_format(now(), '%y-%m-%d')) DAY), INTERVAL -1 WEEK),INTERVAL 6 day);

END IF;  
SET result = CONCAT(start_time,' AND ',end_time) ;
RETURN result;  
END

输入两个参数,一个String作为单位:year、week、month...,另一个int是一个数字,表示往前推多久。(这里关于周和季度做了处理,mysql 一周开始是周日,还有季度 是直接往前推了三个月,达不到我要的效果,我就直接把他写死了~~~~)

我这边的数字输入为负数,因为我用的是
DATE_ADD(date,INTERVAL expr unit) 函数,里面参数分别表示

还有一个DATE_SUB(date,INTERVAL expr unit) ,和上面的用法一样,只不过这个是减去 expr。

贴一个在查询中打的草稿
SELECT date_add(date_format(now(), '%y-01-01'),interval -2 YEAR);

SELECT date_add(date_format(now(), '%y-12-31'),interval 0 YEAR);

SELECT date_add(date_sub(date_format('2018-2-13', '%y-%m-%d'),interval extract(day from '2018-2-13') day),interval 0 MONTH); 

SELECT date_add( DATE_SUB(date_format(now(), '%y-%m-%d'), INTERVAL WEEKDAY(date_format(now(), '%y-%m-%d')) DAY), INTERVAL -2 WEEK);

SELECT date_add(date_add( DATE_SUB(date_format(now(), '%y-%m-%d'), INTERVAL WEEKDAY(date_format(now(), '%y-%m-%d')) DAY), INTERVAL -1 WEEK),INTERVAL 6 day);

SELECT date_add(date_sub(date_format('2017-11-10', '%y-%m-%d'),interval extract(day from '2017-11-10') - 1 day),interval -1 QUARTER)

SELECT date_add(date_sub(date_format('2017-12-10', '%y-%m-%d'),interval extract(QUARTER from '2017-12-10') QUARTER),interval -1 QUARTER)

SELECT extract(QUARTER from now())

SELECT date_sub(date_format('2017-11-10', '%y-%m-%d'),interval extract(day from '2017-11-10')-1 day)

SELECT DATE_ADD(date_sub(date_format('2017-11-10', '%y-%m-%d'),interval extract(day from '2017-11-10')-1 day),INTERVAL -1 QUARTER)

SELECT DATE_ADD('2017-11-01',INTERVAL -1 QUARTER)

SELECT QUARTER(now())

SELECT LAST_DAY('2017-11-01')

SELECT date_add(date_sub(date_format(now(), '%y-01-%d'),interval extract(day from now()) day),interval -1 QUARTER);

都是各种函数的拼接,主要应用就是DATE_ADD(date,INTERVAL expr unit) 这种函数了,围绕他加加减减,就可以实现我们想要的各种时间段了。
~( ̄▽ ̄~)~
[函数表达的意思] (http://www.yiibai.com/mysql/functions.html)

上一篇 下一篇

猜你喜欢

热点阅读