How to Query Between Two Dates Using MySQL

How do I query between two dates using MySQL?

Your second date is before your first date (ie. you are querying between September 29 2010 and January 30 2010). Try reversing the order of the dates:

SELECT *
FROM `objects`
WHERE (date_field BETWEEN '2010-01-30 14:15:55' AND '2010-09-29 10:15:55')

Select data between a date/time range

You need to update the date format:

select * from hockey_stats 
where game_date between '2012-03-11 00:00:00' and '2012-05-11 23:59:00'
order by game_date desc;

MySQL - select data from database between two dates

Your problem is that the short version of dates uses midnight as the default. So your query is actually:

SELECT users.* FROM users 
WHERE created_at >= '2011-12-01 00:00:00'
AND created_at <= '2011-12-06 00:00:00'

This is why you aren't seeing the record for 10:45.

Change it to:

SELECT users.* FROM users 
WHERE created_at >= '2011-12-01'
AND created_at <= '2011-12-07'

You can also use:

SELECT users.* from users 
WHERE created_at >= '2011-12-01'
AND created_at <= date_add('2011-12-01', INTERVAL 7 DAY)

Which will select all users in the same interval you are looking for.

You might also find the BETWEEN operator more readable:

SELECT users.* from users 
WHERE created_at BETWEEN('2011-12-01', date_add('2011-12-01', INTERVAL 7 DAY));

mySQL query between two dates and two times

Not sure if your date field is indexed. If they are then the "concat" examples others have given may not perform very well.

As an alternative you can use a query of the form:

select * 
from foo
where (date > lower_date and date < upper_date) -- technically this clause isn't needed if they are a day apart
or (date = lower_date and time >= lower_time)
or (date = upper_date and time <= upper_time)

It's not pretty but it works and will allow mysql to make use of indexes on the date field if they exist.

So your query would be

SELECT time,
close
FROM intraday_values
where (date > "2005-03-01" and date < "2005-03-02")
or (date = "2005-03-01" and time >= "15:30")
or (date = "2005-03-02" and time <= "15:14")

MYSQL query between two timestamps

Try:

SELECT * 
FROM eventList
WHERE `date` BETWEEN FROM_UNIXTIME(1364256001) AND FROM_UNIXTIME(1364342399)

Or

SELECT * 
FROM eventList WHERE `date`
BETWEEN '2013-03-26 00:00:01' AND '2013-03-26 23:59:59'


Related Topics



Leave a reply



Submit