2014-09-21 130 views
0

我想顯示今天的記錄(從昨天12點到今天11:59 PM),昨天和本週的記錄, 我對今天的記錄有這個查詢獲取今天,昨天和本週的記錄

SELECT COUNT(*) FROM `tblpatients` WHERE `Is_Deleted` = '0' AND `TimeStamp` <= NOW() AND `TimeStamp` >= ????? 

我在一個名爲時間戳格式表有一個字段是獲得昨天2014年9月20日12點十一分20秒

回答

0

如果您僅選擇日期,請根據CURDATE進行計算。

希望下面的例子會幫助你

Today: WHERE timestamp >= CURDATE() 
Yesterday: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND timestamp < CURDATE() 
This month: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE())-1 DAY) 
Between the two dates 3 June 2013 and 7 June 2013 (note how the end date is specified as 8 June, not 7 June): WHERE timestamp >= '2013-06-03' AND timestamp < '2013-06-08' 

請參閱this一個太..

+0

應該是什麼這個星期? – 2014-09-21 04:33:30

+0

使用周函數http://stackoverflow.com/questions/1736010/how-to-group-by-week-in-mysql – 2014-09-21 04:34:25

0

使用strtotime()date()功能。實施例....

$now = date('Y-m-d h:i:s', time()); 
$yesterday = date('Y-m-d h:i:s', strtotime('yesterday')); 
$sql = "SELECT COUNT(*) FROM `tblpatients` WHERE `Is_Deleted` = '0' AND `TimeStamp` <= '$now' AND `TimeStamp` >= '$yesterday'"; 

還可以在query字符串使用BETWEEN。 IE中。

$sql = "SELECT * FROM tblpatients WHERE Is_Deleted = '0' AND TimeStamp BETWEEN '$now' AND '$yesterday'";