1
1
123 1232014-10-18 19:06:02
MySQL
123 123, 2014-10-18 19:06:02

How to write a query to fetch data by time?

Good day to all. There is a table: {id, temperature, humidity, date}. It stores temperature, humidity and date + time (datetime type). Moreover, the weather data is stored in the table every hour. Example: {"id" => 1, "temperature" => 26, "humidity" => 45, "date" => "2014-10-16 12:00:00"}. Help write a query that would return weather and humidity data in the time range from 12:00:00 to 13:00:00 for a week. The range is because there is no such accuracy, the program can record data at 12:00:05. Should be 7 lines. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Entelis, 2014-10-18
@mistergalynsky

SELECT * FROM `mytable`
WHERE
 ( DATE(`date`) BETWEEN "2014-10-13" AND "2014-10-19" )
  AND
 ( TIME(`date`) BETWEEN "12:00:00" AND "13:00:00" )

From myself I will add that it is better not to name the date field, because date is a service word.

B
Be Joy, 2014-10-18
@bejoy

TIMEDIFF will help you
Order definition - ORDER BY `field_name` [ASC|DESC];
Output limit - LIMIT [y,]x;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question