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 :选择 getSeason(myDateField) as Season

于 2014-02-11T13:55:43.013 回答