我是mysql的新手。我想知道如何输出每天向表中添加的记录数。
此数据
loggedID datelogged 1 2015-10-03 2 2015-10-03 3 2015-10-05 4 2015-10-05 5 2015-10-06 6 2015-10-06 7 2015-10-06
会输出
2015-10-03 2 2015-10-04 0 2015-10-05 2 2015-10-06 3
提前致谢
答案 0 :(得分:2)
SELECT datelogged, COUNT(*)
FROM your_table
GROUP BY datelogged
答案 1 :(得分:0)
不确定如何单独使用SQL,所以我使用了一些PHP。输出将转到图表。这就是为什么我需要没有记录调用的日期显示为0。
<?php
# Open the database
require 'config.php';
require 'database_connect.php';
function datediff($date1) {
$date2 = "2015-09-14";
$date1 = date_create($date1);
$date2 = date_create($date2);
$diff=date_diff($date1,$date2);
$realdiff = $diff->format("%a");
return $realdiff;
}
$query = "select datelogged, count(*) as daycalls from rti_loggedcalls group by datelogged";
try
{
$sth = $db->query($query);
while ($row = $sth->fetch (PDO::FETCH_ASSOC))
{
$date = ($row["datelogged"]);
$mdate = datediff($date);
$output[$mdate] = ($row["daycalls"]);
}
}
catch (PDOException $e)
{
printf("We had a problem: %s\n", $e->getMessage());
}
for ($i = 0; $i < ($mdate + 1) ; $i++) {
echo $i." ".(0 + $output[$i])."<br>";
}
?>