简体   繁体   中英

Retrieve records between two dates in mysql?

My query is

select *
from emp 
where DATE_FORMAT(approved_date,'%d-%m-%Y')BETWEEN '01-01-2014' and '26-01-2014';

But it displays the records in the year of 2013.

Table is approved date:

01-10-2013
01-01-2014
25-2-2014

Try this:

select * from emp 
where approved_date 
BETWEEN STR_TO_DATE('18-01-2014', '%d-%m-%Y') 
AND STR_TO_DATE('26-01-2014', '%d-%m-%Y')

Working Demo: http://sqlfiddle.com/#!2/b4d7e/13

MySQL STR_TO_DATE() function

dd-mm-yyyy format

select * from emp 
         where approved_date > STR_TO_DATE('2014-01-01','%d-%m-%Y') and 
               approved_date < STR_TO_DATE('2014-01-25','%d-%m-%Y');  

You can also use Between Too

   select * from emp 
             where approved_date BETWEEN STR_TO_DATE('2014-01-01','%d-%m-%Y')  
                   AND STR_TO_DATE('2014-01-25','%d-%m-%Y');

MySQL STR_TO_DATE Function - Features, Examples and Equivalents

A Sample Example:

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 might also find the BETWEEN operator more readable:

or

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

Hope this helps..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM