2015-10-16 109 views
-1

我是mysql新手。我想知道如何輸出每天添加到表中的記錄數。每天新增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 

在此先感謝

+1

時間去閱讀SQL教程。這是**很**基本的東西。 – Barmar

回答

2
SELECT datelogged, COUNT(*) 
FROM your_table 
GROUP BY datelogged 
+0

答案很接近,但在輸出中沒有「2015-10-04 0」。 – notforsale

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>"; 
    } 
?>