在mysql中检索两个日期之间的记录?

时间:2014-01-17 06:38:23

标签: mysql

我的查询是

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

但它显示了2013年的记录。

表格是批准日期:

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

3 个答案:

答案 0 :(得分:3)

试试这个:

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')

工作演示:http://sqlfiddle.com/#!2/b4d7e/13

答案 1 :(得分:1)

MySQL STR_TO_DATE() function

dd-mm-yyyy格式

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');  

您也可以使用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

答案 2 :(得分:0)

示例示例:

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:

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

希望这会有所帮助..