2014-08-27 67 views
0

我们有一个名为created的列,它是一个mysql时间戳。我必须选择在前一天的下午5点到当天下午4:59之间创建的记录。在mysql中选择具体时间

假设我将日期范围为25-8-2014传递给该程序,它应该将所有创建的记录在24-8- 2014 5:00 PM至25-8-2014 4:49 PM之间创建。我们如何编写查询来做到这一点?

回答

0

您可以使用BETWEEN子句。 BETWEEN运算符选择一个范围内的值。这些值可以是数字,文本或日期。

语法:

SELECT column_name(s) 
FROM table_name 
WHERE column_name BETWEEN value1 AND value2; 

例子:

Select * from tableNawhere created between date1 and date2; 

注:在你的情况,date1(date2 - 1day)

+0

错误,如果日期2 = DATE1 - 第1天,将取回0记录。还要注意,这是包容性的。我怀疑,OP每次执行查询时都想手动插入日期。 – fancyPants 2014-08-27 11:57:26

1
select * from your_table 
where created between 
curdate() - interval 1 day + interval 17 hour 
and 
curdate() + interval 16 hour + interval 59 minute; 

select * from your_table 
where created between 
date_format(now() - interval 1 day, '%Y-%m-%d 17:00:00') 
and 
date_format(now(), '%Y-%m-%d 16:59:00'); 

select * from your_table 
where created between 
concat(curdate() - interval 1 day, ' 17:00:00') /*note the whitespace before 17*/ 
and 
concat(curdate(), ' 16:59:59');