2014-02-11 29 views
0

我對MySQL數據庫非常陌生。在Excel中,我有一個公式,它查看日期單元格,然後根據該行數據來自哪個季節。來自Array/Formula的MySQL中的計算列

在Excel中我使用這個公式看起來在月份和日期,以確定其是否從數組它落入值:

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

有沒有辦法設置一個視圖或標籤這個到MySQL中的表爲了更新本身的季節?非常感謝您的採訪。

回答

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 :選擇getSeason(myDateField)爲季

+0

WOW - 更多的技術比我想象的,我感謝你非常努力,並會在今天晚上發佈:0) – Palendrone

+0

如果它對你有用,請將答案標記爲已接受,以便未來的訪問者知道它的工作(並且我得到一些獎勵;)。 –