Search code examples
mysqlsqldatabasedatetimerdbms

Why I can't obtain record in a range of date?


I am not so into SQL and I have to create a complex query. My idea is start to a simple query and enrich it step by step. I am using MySql

I ahve some doubts about a possible smart way for the first step.

So I have a MeteoForecast table like this:

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;

It contains data like this:

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                                                                                                                                                                                                                                                 

It represents meteo forecast and as you can see it have a starting datetime and and ending datetime. So, for a specific place (specified by the localization_id field) I can have more record for a same day (different hours range, in the example I created 2 range: from 06:00 to 12:00 and from 12:00 to 18:00).

I want retrieve all the records related 2 days from a starting date so I am trying to do something like this:

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;

But it give me back 0 records.

I also tried this:

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;

But still obtain the same problem

What could be the problem? What am I missing? How can I fix this issue?


Solution

  • Try this.

    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 in Mysql. Refer this doc.