mySQL:在结果中的间隔之间自动插入行

时间:2011-10-19 07:19:26

标签: php mysql business-logic

我的任务是提取按小时分组的数据。我通过发出类似下面的命令来做到这一点:

SELECT DISTINCT 
   COUNT(player_id) AS `count`, 
   YEAR(date_created) AS `year`, 
   MONTH(date_created) AS `month`, 
   DAY(date_created) AS `day`, 
   HOUR(date_created) AS `hour` 

FROM `logs` 

WHERE 
   (controller='player') AND 
   (action='updatehash') AND (date_created >= FROM_UNIXTIME(1317916800)) AND 
   (date_created <= FROM_UNIXTIME(1318003199)) 

GROUP BY `year`, `month`, `day`, `hour` 

ORDER BY `year` ASC, `month` ASC, `day` ASC, `hour` ASC 

返回类似于以下结果的结果:

Array
(
    [0] => Array
        (
            [count] => 2
            [year] => 2011
            [month] => 10
            [day] => 7
            [hour] => 16
        )

    [1] => Array
        (
            [count] => 5
            [year] => 2011
            [month] => 10
            [day] => 7
            [hour] => 17
        )

    [2] => Array
        (
            [count] => 21
            [year] => 2011
            [month] => 10
            [day] => 7
            [hour] => 18
        )

    [3] => Array
        (
            [count] => 3
            [year] => 2011
            [month] => 10
            [day] => 7
            [hour] => 19
        )

)

结果似乎还可以,我必须让它返回过去24小时的结果,包括没有计数值的小时数。因此,根据我上面公布的结果,它应该将结果从10月7日19:00返回到10月6日18:00。

这可能吗?

谢谢大家! ;)

1 个答案:

答案 0 :(得分:1)

简短的回答,没有使用这个SQL,你没有加入表,所以你不能使用“左连接”技术。这是因为你在那段时间里确实没有任何记录记录。

您可以尝试使用编程语言创建FUNCTION方法或每小时插入至少一条记录。