2014-08-27 69 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');