0

私はMySQLデータベースに非常に慣れていません。Excel には、日付セルを見て、データの行がどの季節からのものかを示す式があります。

Excelでは、月と日を調べて、配列の値がどちらに該当するかを判断する次の式を使用します。

=LOOKUP(TEXT([DATE_CELL],"mmdd"),{"0101","0321","0621","0922","1221";"Winter","Spring","Summer","Autumn","Winter"})

ビューをセットアップするか、これをMySQLのテーブルにタグ付けしてシーズンを更新する方法はありますか? ご覧いただきありがとうございます。

4

1 に答える 1

1

次のような関数を作成します。

CREATE DEFINER = 'root'@'localhost' FUNCTION `getSeason`(P_date DATE)
    RETURNS varchar(10) CHARSET latin1
    DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
DECLARE v_season VARCHAR(10);
DECLARE v_month integer;
DECLARE v_day integer;
select date_format(P_date,'%m'),date_format(P_date,'%d') into v_month,v_day;
if (v_month<3 or (v_month=3 and v_day<21)) then 
     set  v_season="Winter";
else if (v_month<6 or (v_month=6 and v_day>=21)) then 
     set  v_season="Spring";
else if (v_month<9 or (v_month=9 and v_day>=21)) then 
    set   v_season="Summer";
else if (v_month<12 or (v_month=12 and v_day<=21)) then 
    set   v_season="Autumn";
else
 set  v_season="Winter";
end if;
end if;
end if;
end if;
  RETURN v_season;
END;

そして、WHERE 句で関数を使用します: where getSeason(myDaeField)='Winter' Or in your select :select getSeason(myDateField) as Season

于 2014-02-11T13:55:43.013 に答える