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

发布于 2024-12-11 00:37:14 字数 1460 浏览 0 评论 0原文

我的任务是提取按小时分组的数据。我通过发出类似于下面的命令来执行此操作:

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 的结果。

这可能吗?

谢谢大家! ;)

My task is to extract data grouped by hours. I do this by issuing a command similar to the one below:

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 

Which returns results similar to the one below:

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
        )

)

The result seem to be OK only that I have to have it return results for the last 24 hours including hours that no count values. So, based on the result I posted above, it should return results from Oct 7, 19:00 down to Oct 6, 18:00.

Is this possible?

Thank you everyone! ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2024-12-18 00:37:14

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

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

Short answer, no using this SQL, you are not joining table, so you can not use the "left join" technique. it is because you really don't have any logging record in that period of time.

May be you can either try create FUNCTION approach or insert at least one record each hour using your programming language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文