1

フィールドに保存されている日付を取得し、それを過去 12 か月の日付として自動的に処理するSELECTクエリを実行したいと考えています。tableyear_end

つまり、今日は 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;

例として選択した 3 つの日付と 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> set @mydate = '2012-01-01'; クエリ OK、影響を受ける行は 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 に答える