CREATE TABLE MeteoForecast (
id BigInt(20) NOT NULL AUTO_INCREMENT,
localization_id BigInt(20) NOT NULL,
seasonal_forecast_id BigInt(20),
meteo_warning_id BigInt(20),
start_date DateTime NOT NULL,
end_date DateTime NOT NULL,
min_temp Float,
max_temp Float,
icon_link VarChar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
PRIMARY KEY (
id
)
) ENGINE=InnoDB;
它包含一些类似的数据:
id localization_id start_date end_date min_temp max_temp icon_link
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 1 18/09/2017 06:00:00 18/09/2017 12:00:00 15 24 Mostly_Cloudy_Icon.png
2 1 18/09/2017 12:00:00 18/09/2017 18:00:00 15 24 Light_Rain.png
3 1 19/09/2017 06:00:00 19/09/2017 12:00:00 12 22 Mostly_Cloudy_Icon.png
4 1 19/09/2017 12:00:00 19/09/2017 18:00:00 13 16 Mostly_Cloudy_Icon.png
5 1 20/09/2017 06:00:00 20/09/2017 12:00:00 18 26 Light_Rain.png
6 1 20/09/2017 12:00:00 20/09/2017 18:00:00 17 25 Light_Rain.png
因此,正如您在上一个数据集中所看到的,每条记录都有一个起始日期时间和结束日期时间。这是因为我在特定的一天收集了更多的预测信息(它基于时间范围,在每天的例子中,记录从早上06:00到12:00,另一条记录从12:00到18:00)
如您所见,我创建了此查询,将信息提取到 DateTime 的特定范围内,这是我的查询代码:
SELECT
*
FROM MeteoForecast as MF
WHERE
MF.start_date between '2017-09-18 06:00:00' and '2017-09-20 12:00:00'
它工作正常,但我需要通过以下方式更改此子句:
所以基本上我想把查询的起始日期和一个数字传递给查询,这个数字代表了这个开始日之后的天数。
如何更改查询以实现此行为?
答案 0 :(得分:5)
您可以在BETWEEN
中使用DATE_ADD
,如下所示:
WHERE MF.start_date between '2017-09-18 06:00:00'
AND DATE_ADD('2017-09-18 06:00:00', INTERVAL 10 DAY)
因此,在这种情况下,您只需要两个参数start date
和传递给查询的日期间隔。
答案 1 :(得分:1)
你应该使用date_add函数(在MySQL中,这是你的sql平台):
SELECT
MF.start_date,
MF.min_temp,
MF.max_temp,
MF.icon_link,
MFD.id,
MFD.meteo_forecast_description_name,
MFD.description
FROM MeteoForecast as MF
INNER JOIN MeteoForecast_MeteoForecastDescription as MF_MFD
ON MF.id = MF_MFD.meteo_forecast_id
INNER JOIN MeteoForecastDescription as MFD
ON MF_MFD.meteo_forecast_description_id = MFD.id
INNER JOIN Languages AS LNG
ON MFD.language_id = LNG.id
WHERE
MF.start_date between '2017-09-18 06:00:00' and DATE_ADD(MF.start_date, INTERVAL 4 DAY)
AND LNG.id = 1