Query MySQL Table For Records Changed Between Time Period

Goal: Search all users who were updated between a certain time period (in our case one day).

Scenario: A system wide glitch caused a set of users to have all of their last names changed to Smith. Your manager wants to know how many records are affected.

MySQL offers 2 or more ways this can be accomplished. First using <= and >=  the second using BETWEEN operator.

The between operator is identical to >= and <= and is inclusive. Notice the difference in order between the 2 queries.

In order for this query to work the field must be either datetime, timestamp, or date. If using date then remove the time portion from the query.

select * from users where updated_at <= '2014-12-25 23:59:59' AND updated_at >= '2014-12-25 00:00:00';
select * from users where updated_at BETWEEN '2014-12-25 00:00:00' AND '2014-12-25 23:59:59';

Demo on SQL Fiddle.

Share this Story
Load More Related Articles
Load More By Nick Escobedo
Load More In How-To's

Check Also

Understanding Trade-offs as a Developer

Trade-offs are one of the most important aspects ...