为什么我无法获取某个日期范围内的记录?
Why I can't obtain record in a range of date?
我不太喜欢 SQL,我必须创建一个复杂的查询。我的想法是从一个简单的查询开始,逐步丰富它。我正在使用 MySql
我对第一步可能的聪明方法有一些疑问。
所以我有一个 MeteoForecast table 这样的:
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 AUTO_INCREMENT=3 ROW_FORMAT=DYNAMIC DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
它包含这样的数据:
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
它代表气象预报,如您所见,它有开始日期时间和结束日期时间。因此,对于特定地点(由 localization_id 字段指定)我可以在同一天有更多记录(不同的时间范围,在示例中我创建了 2 个范围:从06:00 到 12:00 以及从 12:00 到 18:00).
我想检索从开始日期起 2 天后的所有相关记录,所以我正在尝试这样做:
select * from MeteoForecast where
start_date between '18/09/2017 06:00:00' and '20/09/2017 06:00:00'
order by start_date desc;
但是它给了我 0 条记录。
我也试过这个:
select * from MeteoForecast where
start_date >= '18/09/2017 06:00:00' and
end_date < '20/09/2017 18:00:00'
order by start_date desc;
但是还是得到同样的问题
可能是什么问题?我错过了什么?我该如何解决这个问题?
试试这个。
select * from MeteoForecast
where start_date between '2017-09-18 06:00:00' and '2017-09-20 06:00:00'
order by start_date desc;
Datetime Mysql。请参阅此文档。
我不太喜欢 SQL,我必须创建一个复杂的查询。我的想法是从一个简单的查询开始,逐步丰富它。我正在使用 MySql
我对第一步可能的聪明方法有一些疑问。
所以我有一个 MeteoForecast table 这样的:
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 AUTO_INCREMENT=3 ROW_FORMAT=DYNAMIC DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
它包含这样的数据:
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
它代表气象预报,如您所见,它有开始日期时间和结束日期时间。因此,对于特定地点(由 localization_id 字段指定)我可以在同一天有更多记录(不同的时间范围,在示例中我创建了 2 个范围:从06:00 到 12:00 以及从 12:00 到 18:00).
我想检索从开始日期起 2 天后的所有相关记录,所以我正在尝试这样做:
select * from MeteoForecast where
start_date between '18/09/2017 06:00:00' and '20/09/2017 06:00:00'
order by start_date desc;
但是它给了我 0 条记录。
我也试过这个:
select * from MeteoForecast where
start_date >= '18/09/2017 06:00:00' and
end_date < '20/09/2017 18:00:00'
order by start_date desc;
但是还是得到同样的问题
可能是什么问题?我错过了什么?我该如何解决这个问题?
试试这个。
select * from MeteoForecast
where start_date between '2017-09-18 06:00:00' and '2017-09-20 06:00:00'
order by start_date desc;
Datetime Mysql。请参阅此文档。