在MySQL中按分钟生成连续的date_time

时间:2019-01-19 09:35:05

标签: mysql date datetime time

我正在使用以下在网上找到的MySQL来生成日期列表。 但是,我希望它按分钟生成date_times列表,例如yyyy-mm-dd hh:mm:ss。 这可能吗?我无法确定现有SQL是如何生成列表的,没关系增加分钟!

对于图表/图形的基本查询,我需要使用此方法,使用左联接从其他位置提取数据。

谢谢

select * from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2019-01-01' and '2019-01-15'

当前输出:

selected_date
---
2019-01-01
2019-01-02
2019-01-03
2019-01-04

所需的输出:

selected_date
---
2019-01-19 00:05:00
2019-01-19 00:06:00
2019-01-19 00:07:00
2019-01-19 00:08:00
2019-01-19 00:09:00
2019-01-19 00:10:00
2019-01-19 00:11:00

编辑: 这是我的SQL查询提供图表。我每分钟会得到5-6条温度记录,因此我将它们平均。 我的问题有时是温度探测器脱落,因此a.date_time或b.date_time没有每分钟的连续date_time

    select 
        b.date_time,
        a.temperature as fishtank,
        b.temperature as room
    from (
        select
            serial,
            convert((min(date_time) div 100)*100, datetime)  as date_time, 
            avg(temperature) as temperature
        from
            raspicontroller.temperature
        WHERE 
            date_time between '$date_from' and '$date_to' and 
            serial like '28-000898430d59'
        group by 
            date_time div 100
    ) a
    right join (
        select
            serial,
            convert((min(date_time) div 100)*100, datetime)  as date_time, 
            avg(temperature) as temperature
        from
            raspicontroller.temperature
        WHERE 
            date_time between '$date_from' and '$date_to' and 
            serial like '28-000f9843201e'
        group by 
            date_time div 100
    ) b on a.date_time = b.date_time

再次编辑: 我没有设法生成想要的内容,但是我想出了它使用数据库中的现有值来列出日期的方法。我认为这对我有用

SELECT
    a.date_time,
    b.temperature as FishTank,
    c.temperature as Room
FROM (
    select
        convert((min(date_time) div 100)*100, datetime)  as date_time
    from
        raspicontroller.temperature
    group by 
        date_time div 100
) as a
LEFT JOIN (
    select
        serial,
        convert((min(date_time) div 100)*100, datetime)  as date_time, 
        avg(temperature) as temperature
    from
        raspicontroller.temperature
    WHERE
        serial like '28-000898430d59'
    group by 
        date_time div 100
) as b on a.date_time = b.date_time
LEFT JOIN (
    select
        serial,
        convert((min(date_time) div 100)*100, datetime)  as date_time, 
        avg(temperature) as temperature
    from
        raspicontroller.temperature
    WHERE
        serial like '28-000f9843201e'
    group by 
        date_time div 100
) as c on a.date_time = c.date_time

0 个答案:

没有答案