我想在SELECT
上运行table
查询,该查询会获取year_end
字段中存储的日期,并自动将其视为过去12个月内该日期的发生
即:今天是5月14日所以:
如果我有2012-01-01
,那么我希望保持不变
如果我有2010-05-05
,那么我想要2012-05-05
如果我有2012-10-10
,那么我想要2011-10-10
我需要SELECT
的任何想法吗?
答案 0 :(得分:2)
以下是您的需求:
select
concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',
right(@mydate,5)),right(@mydate,5))) dttm
from
(
select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
(select substr(date(@mydate),6) my_md) AA,
(select substr(date(now()),6) md,year(now()) yr) BB
) A;
以下是您选择的三个日期作为示例+ 2个闰年日期
set @mydate = '2012-01-01';
set @mydate = '2010-05-05';
set @mydate = '2012-10-10';
set @mydate = '2008-02-29';
set @mydate = '2012-02-29';
这里他们被执行
的MySQL>设置@mydate ='2012-01-01'; 查询正常,0行受影响(0.00秒)
mysql> select
-> concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',right(@mydate,5)),right(@mydate,5))) dttm
-> from
-> (
-> select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
-> IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
-> (select substr(date(@mydate),6) my_md) AA,
-> (select substr(date(now()),6) md,year(now()) yr) BB
-> ) A;
+------------+
| dttm |
+------------+
| 2012-01-01 |
+------------+
1 row in set (0.00 sec)
mysql> set @mydate = '2010-05-05';
Query OK, 0 rows affected (0.00 sec)
mysql> select
-> concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',right(@mydate,5)),right(@mydate,5))) dttm
-> from
-> (
-> select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
-> IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
-> (select substr(date(@mydate),6) my_md) AA,
-> (select substr(date(now()),6) md,year(now()) yr) BB
-> ) A;
+------------+
| dttm |
+------------+
| 2012-05-05 |
+------------+
1 row in set (0.00 sec)
mysql> set @mydate = '2012-10-10';
Query OK, 0 rows affected (0.00 sec)
mysql> select
-> concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',right(@mydate,5)),right(@mydate,5))) dttm
-> from
-> (
-> select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
-> IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
-> (select substr(date(@mydate),6) my_md) AA,
-> (select substr(date(now()),6) md,year(now()) yr) BB
-> ) A;
+------------+
| dttm |
+------------+
| 2012-10-10 |
+------------+
1 row in set (0.00 sec)
mysql> set @mydate = '2008-02-29';
Query OK, 0 rows affected (0.00 sec)
mysql> select
-> concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',right(@mydate,5)),right(@mydate,5))) dttm
-> from
-> (
-> select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
-> IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
-> (select substr(date(@mydate),6) my_md) AA,
-> (select substr(date(now()),6) md,year(now()) yr) BB
-> ) A;
+------------+
| dttm |
+------------+
| 2012-02-29 |
+------------+
1 row in set (0.00 sec)
mysql> set @mydate = '2012-02-29';
Query OK, 0 rows affected (0.00 sec)
mysql> select
-> concat(yr,'-',if(leapyear=0,if(right(dt,5)='02-29','03-01',right(@mydate,5)),right(@mydate,5))) dttm
-> from
-> (
-> select concat(if(my_md > md,yr -1,yr),'-',my_md) dt,yr,my_md,
-> IF(MOD(yr,4)>0,0,IF(MOD(yr,100),1,(MOD(yr,400)=0))) leapyear FROM
-> (select substr(date(@mydate),6) my_md) AA,
-> (select substr(date(now()),6) md,year(now()) yr) BB
-> ) A;
+------------+
| dttm |
+------------+
| 2012-02-29 |
+------------+
1 row in set (0.00 sec)
mysql>
试一试!!!
答案 1 :(得分:1)
Probbaly效率低下的查询,关于性能,但它有效:
SELECT
dateX + INTERVAL ( YEAR(CURDATE())
- YEAR(dateX)
- ( ( MONTH(dateX), DAY(dateX) )
> ( MONTH(CURDATE()), DAY(CURDATE()) )
)
) YEAR
AS calc_date
FROM
tableX ;
您可以对其进行测试SQL-Fiddle。另一个版本,稍微更具可读性:
SELECT
d + INTERVAL ( now.yy - t.yy - ((t.mm, t.dd) > (now.mm, now.dd))
) YEAR
AS calc_date
FROM
( SELECT
dateX AS d
, YEAR(dateX) AS yy
, MONTH(dateX) AS mm
, DAY(dateX) AS dd
FROM tableX
) AS t
CROSS JOIN
( SELECT
YEAR(CURDATE()) AS yy
, MONTH(CURDATE()) AS mm
, DAY(CURDATE()) AS dd
) AS now ;