我正在开发一个系统,它应该输出一天内发送的短信总数。它还应该分开交付的和失败的。 它应该只将那天的记录放在那里......就像那天的记录一样。 我的数据库是smsdb。
$sql = "CREATE TABLE smsdb (
id INT(6) AUTO_INCREMENT PRIMARY KEY,
sms_text VARCHAR(300) NOT NULL,
receiver_number VARCHAR(30) NOT NULL,
time_sent DATETIME,
status VARCHAR(20) )";
//I tried to use the command to display todays records but I get all records even for other days.
$sql = ("SELECT DATE(time_sent) AS date, COUNT(*) as total_sent, SUM(status = 'Success') as delivered,SUM(status = 'failed') AS failed FROM smsdb GROUP BY DATE(time_sent) ");
我只想要今天的总记录。 亲切的是,有人帮助我
答案 0 :(得分:1)
您没有加入日期过滤器。您可以将其作为变量传递,因此您可以选择其他日期:
$sql = ("SELECT DATE(time_sent) AS date, COUNT(*) as total_sent, SUM(status = 'Success') as delivered,SUM(status = 'failed') AS failed FROM smsdb WHERE DATE(time_sent) = '$date_selected'");
请注意,您确实应该为此绑定参数,但这会给您一个想法。
如果您总是希望它是当天的另一个选项,请使用CURDATE()
功能:
$sql = ("SELECT DATE(time_sent) AS date, COUNT(*) as total_sent, SUM(status = 'Success') as delivered,SUM(status = 'failed') AS failed FROM smsdb WHERE DATE(time_sent) = CURDATE()");
答案 1 :(得分:1)
只需过滤当前日期的where条件:
SELECT
DATE(time_sent) AS date,
COUNT(*) as total_sent,
SUM(case when status = 'Success' then 1 else 0 end) as delivered,
SUM(case when status = 'failed' then 1 else 0 end) AS failed
FROM smsdb
WHERE date(time_sent)=curdate()
GROUP BY DATE(time_sent)