1

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?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • @Strawberry why? I also need time into date. I think that with simple dates I can't store also the time... – AndreaNobili Sep 18 '17 at 15:41
  • 3
    @Strawberry he gives the table definition - he's storing dates as dates. He needs to *query* dates with dates. Possible duplicate of https://stackoverflow.com/questions/1509977/convert-varchar-into-datetime-in-sql-server – Jeutnarg Sep 18 '17 at 15:42
  • @Strawberry looks like start_date and end_date are datetime to me. OP maybe you have a code page issue? Try with a convert to both dates to see. Also, BETWEEN is really confusing as it is inclusive so when you use it with dates you have to be very careful. – Jacob H Sep 18 '17 at 15:42
  • could be the date format of the strings in your query? Convert them to datetimes as you run the query, and/or use ISO strings to avoid ambiguous dates (e.g. 01/03 could be 1st March or 3rd Jan depending on your locale). Also ensure you're using a consistent timezone in your DB. – ADyson Sep 18 '17 at 15:43
  • @JacobH OK - well, the OP seems to be comparing non-conforming strings with dates (unless there were 2017 days in year 18 AD). – Strawberry Sep 18 '17 at 15:44
  • @Jeutnarg - he's using mysql, so this one's probably the better dupe target https://stackoverflow.com/questions/5201383/how-to-convert-a-string-to-date-in-mysql – Jeutnarg Sep 18 '17 at 15:44

1 Answers1

3

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.

Hatim Stovewala
  • 1,333
  • 10
  • 19