我没有测试过这一点,但你可以做类似如下:
SELECT
# check if the year is a leap year:
IF((YEAR(mydate) % 4 = 0 AND YEAR(mydate) % 100 != 0) OR YEAR(mydate) % 400 = 0,
# if so, check if the date is before or after february 29th. if that is the case, we subtract one from the value
IF(DAYOFYEAR(mydate) > DAYOFYEAR("2008-02-28"), DAYOFYEAR(mydate) - 1, DAYOFYEAR(mydate)),
# if the year isn't a leap year, just return the regular dayofyear() value
DAYOFYEAR(mydate))
FROM mytbl
这将合并为28日和29日为闰年的数据,但在闰年和非闰年的所有日子里给了天一样的偏移。更理想的行为可能是简单地忽略2月29日的数据,这可以使用附加条件来完成。你也可以为它分配一个特殊的索引,比如400,这不会抵消其他所有的日子。
一个更好的办法可能是由组每月月和日:
select month(date) m, day(date) d, count(*) from reviews group by m, d;
这一起避免了这个问题,但可能会更加复杂,你在你的应用程序逻辑处理。不过,我认为这是一个更好的方法来做到这一点。