mysql按天过滤

时间:2014-10-03 08:32:48

标签: php mysql date

我正在尝试仅显示今天结果的信息。 我正在使用的以下示例在24小时内显示信息。

我只需要显示当天的信息。

示例:

$sql = "SELECT COUNT(*) as c 
        FROM toutcome 
        WHERE AffID = '$affID' AND 
        CompletedDate > DATE_SUB(CURDATE(), INTERVAL 1 DAY) ";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo  $row['c'] ; 

我也试过

$sql = "SELECT COUNT(*) as c 
        FROM toutcome 
        WHERE AffID = '$affID' AND 
        CompletedDate > DATE_SUB(CURDATE(), INTERVAL 24 HOUR) ";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo  $row['c'] ; 

2 个答案:

答案 0 :(得分:0)

试试这个

SELECT COUNT(*)
 as c FROM toutcome 
WHERE AffID = '$affID' 
AND 
TIMESTAMPDIFF(
    DAY,
    DATE(CompletedDate),
    DATE(CURRENT_TIMESTAMP)
) = 1

答案 1 :(得分:0)

尝试:

SELECT COUNT(*) as c FROM toutcome WHERE AffID = '$affID' AND CompletedDate BETWEEN CURDATE() AND NOW()

如果您知道CompletedDate将来永远不会出现:

SELECT COUNT(*) as c FROM toutcome WHERE AffID = '$affID' AND CompletedDate >= CURDATE()

Arun的解决方案不会使用CompletedDate上的索引。