1

我想运行一个SELECT查询table,获取存储在year_end字段中的日期并自动将其视为过去 12 个月中出现的日期。

即:今天是 5 月 14 日,所以:

如果我有2012-01-01,那么我希望保持不变

如果我有2010-05-05那么我想要2012-05-05

如果我有2012-10-10那么我想要2011-10-10

请对SELECT我需要的任何想法?

4

2 回答 2

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>

试试看 !!!

于 2012-05-14T17:46:34.410 回答
1

关于性能的查询可能效率低下,但它有效:

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 ;
于 2012-05-14T18:20:16.563 回答