选择DATETIME = DATE的行计数

时间:2019-06-09 22:54:21

标签: mysql count

此查询将DATETIME转换为DATE,并返回同一DATE的行数。

select*, DATE(call_end) AS Date,
COUNT(*) AS num_emails
from mk_calls mkc 
where call_end >= CURDATE() - INTERVAL 10 DAY
GROUP BY DATE(call_end)
ORDER BY call_start

我想知道在知道日期后如何执行类似的查询吗?这可行吗?

$today_date = '2019-06-09';
//perform count query here

1 个答案:

答案 0 :(得分:2)

您可以使用MySQLi准备好的语句轻松地做到这一点。

$sql = 'SELECT *, DATE(call_end) AS Date, COUNT(*) AS num_emails
    FROM mk_calls mkc 
    WHERE call_end >= ?
    GROUP BY DATE(call_end)
    ORDER BY call_start';

$today_date = '2019-06-09';
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $today_date);
$stmt->execute();

使用SELECT *通常是个坏主意,特别是如果您使用GROUP BY