Mysql 为什么此查询(将预测返回到时间范围)在月份更改时返回空结果集?
我不太喜欢数据库,这个从表中提取一些记录的查询有以下问题。我正在使用MySql 我有一个名为MeteoForecast的主表,其中包含以下meteo forecast:Mysql 为什么此查询(将预测返回到时间范围)在月份更改时返回空结果集?,mysql,sql,database,rdbms,Mysql,Sql,Database,Rdbms,我不太喜欢数据库,这个从表中提取一些记录的查询有以下问题。我正在使用MySql 我有一个名为MeteoForecast的主表,其中包含以下meteo forecast: INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (17, 1, NULL, NUL
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (17, 1, NULL, NULL, '2017/11/27 06:00:00.000', '2017/11/27 12:00:00.000', 27, 33, 'Light_Rain.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (18, 1, NULL, NULL, '2017/11/27 12:00:00.000', '2017/11/27 18:00:00.000', 35, 29, 'Mostly_Cloudy_Icon.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (19, 1, NULL, NULL, '2017/11/28 06:00:00.000', '2017/11/28 12:00:00.000', 26, 31, 'Light_Rain.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (20, 1, NULL, NULL, '2017/11/28 12:00:00.000', '2017/11/28 18:00:00.000', 33, 27, 'Mostly_Cloudy_Icon.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (21, 1, NULL, NULL, '2017/10/13 12:00:00.000', '2017/10/13 18:00:00.000', 18, 22, 'Light_Rain.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (22, 1, NULL, NULL, '2017/11/29 06:00:00.000', '2017/11/29 12:00:00.000', 30, 34, 'Light_Rain.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (23, 1, NULL, NULL, '2017/11/29 12:00:00.000', '2017/11/29 18:00:00.000', 34, 31, 'Light_Rain.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (24, 1, NULL, NULL, '2017/11/30 06:00:00.000', '2017/11/30 12:00:00.000', 29, 32, 'Mostly_Cloudy_Icon.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (25, 1, NULL, NULL, '2017/11/30 12:00:00.000', '2017/11/30 18:00:00.000', 33, 31, 'Mostly_Cloudy_Icon.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (26, 1, NULL, NULL, '2017/12/01 06:00:00.000', '2017/12/01 12:00:00.000', 27, 31, 'Light_Rain.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (27, 1, NULL, NULL, '2017/12/01 12:00:00.000', '2017/12/01 18:00:00.000', 34, 29, 'Mostly_Cloudy_Icon.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (28, 1, NULL, NULL, '2017/12/02 06:00:00.000', '2017/12/02 12:00:00.000', 28, 34, 'Light_Rain.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (29, 1, NULL, NULL, '2017/12/02 12:00:00.000', '2017/12/02 18:00:00.000', 34, 31, 'Light_Rain.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (30, 1, NULL, NULL, '2017/12/03 06:00:00.000', '2017/12/03 12:00:00.000', 26, 29, 'Mostly_Cloudy_Icon.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (31, 1, NULL, NULL, '2017/12/03 12:00:00.000', '2017/12/03 18:00:00.000', 29, 33, 'Mostly_Cloudy_Icon.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (32, 1, NULL, NULL, '2017/12/04 06:00:00.000', '2017/12/04 12:00:00.000', 25, 29, 'Mostly_Cloudy_Icon.png');
INSERT INTO MeteoForecast (id, localization_id, seasonal_forecast_id, meteo_warning_id, start_date, end_date, min_temp, max_temp, icon_link) VALUES (33, 1, NULL, NULL, '2017/12/04 12:00:00.000', '2017/12/04 18:00:00.000', 30, 33, 'Light_Rain.png');
如您所见,这些记录以11月有值的开始日期字段开始,以12月有值的开始日期字段结束
现在我有了这个查询,它提取了与从今天开始的未来7天的预测相关的记录:
SELECT
DATE_FORMAT(MF.start_date, GET_FORMAT(DATETIME,'ISO')) AS start_date ,
MF.id AS meteo_forecast_id ,
MF.min_temp AS min_temp ,
MF.max_temp AS max_temp ,
MF.icon_link AS icon_link ,
MFD.id AS meteo_forecast_description_id ,
MFD.meteo_forecast_description_name AS meteo_forecast_description_name ,
MFD.description AS meteo_forecast_description ,
MFD.audio_link AS meteo_forecast_description_audio_link,
MA.id AS meteo_advice_id ,
MA.description AS meteo_advice_description ,
CR.id AS crop_id ,
CR.crop_name AS crop_name ,
MA.planting_date AS planting_date ,
MA.harvesting_date AS harvesting_date ,
MA.water_needs AS water_needs ,
MA.audiolink AS meteo_advice_audiolink
FROM
MeteoForecast as MF
LEFT JOIN
MeteoForecast_MeteoForecastDescription as MF_MFD
ON
MF.id = MF_MFD.meteo_forecast_id
LEFT JOIN
MeteoForecastDescription as MFD
ON
MF_MFD.meteo_forecast_description_id = MFD.id
LEFT JOIN
Languages AS LNG
ON
MFD.language_id = LNG.id
AND LNG.id = 1
LEFT JOIN
MeteoAdvice AS MA
ON
MF.id = MA.meteo_forecast_id
LEFT JOIN
Crop AS CR
ON
MA.crop_id = CR.id
WHERE
MF.localization_id = 1
AND MF.start_date >= CURDATE()
AND MF.end_date <= CURDATE()+7
ORDER BY
MF.start_date
它会返回这样的结果:2017年11月27日,但如果我尝试以这种方式更改它(添加天数):
该值不再是一个标准日期,但它是这样的:20171131
因此,当它现在尝试计算此表达式时,可能有问题:
AND MF.end_date <= CURDATE()+4
和MF.end_date由我自己解决,用此代码更改第二个WHERE子句:
AND MF.end_date <= ADDDATE(CURDATE(), 7)
和MF.end\u日期由于需要滚动代码,我建议阅读。
CURDATE() AS cur_date,
CURDATE() + 4 AS cur_date,
AND MF.end_date <= CURDATE()+4
AND MF.end_date <= ADDDATE(CURDATE(), 7)