zoukankan      html  css  js  c++  java
  • mysql时间操作函数和存储过程

    因为业务须要统计一批数据。用到关于mysql的时间操作函数和存储过程,问题已经基本解决。把过程记录下:

    1. mysql的语句中不支持直接用循环。循环仅仅能在存储过程中使用。

    2. 写为文件时,注意一些隐藏的字符,造成语法错误。本例中凝视中包括一些不可见字符。没有找到。

    3. 存储过程中尽量多使用分好,切割开语句。

    本例中 drop一句最初没有写。导致一直有错。

    4. 时间函数非常强大,能够避免一些工作。http://www.cnblogs.com/ggjucheng/p/3352280.html

    5. sql语句的优化非常重要。本例中仅攻克了问题,但数据量太大。存储过程执行了非常久。期待有人能帮忙优化while语句中的优化。

    以下贴代码了:

    --*将表test.transport20140901表中的数据依照每五分钟一个间隔,统计各个路口的车流数量r
    --*@start_time 起始时间 是整点时间的五分钟间隔 如 2014-09-01 00:20:00
    --*@end_time  终止时间  是整点的五分钟间隔且大于start_time  如 2014-09-01 01:00:00
    --*统计范围包括起始时间,但不包括终止时间
    
    
    delimiter $
    drop procedure transport_status;
    create procedure transport_status(start_time datetime,end_time datetime)
    begin
            declare mid_start_time datetime;
            declare mid_end_time datetime;
            set mid_start_time=start_time;
            set mid_end_time=date_add(start_time, interval 5 minute);
            lab: while mid_start_time < end_time do
                    insert into
                            test.transport_status(stamp,stamp_time,address,car_count)
                            (select
                                    FLOOR(UNIX_TIMESTAMP(time)/300) as stmp,
                                    date_format(mid_end_time,'%Y-%m-%d %H:%i:%s') as tm,
                                    address,
                                    count(address) as cnt
                            from
                            test.transport20140901
                            where
                            time > date_add(mid_start_time, interval -1 second)
                            and time < mid_end_time
                            group by address);
                    set mid_start_time=date_add(mid_start_time, interval 5 minute);
                    set mid_end_time=date_add(mid_end_time, interval 5 minute);
            end while lab;
    end $
    delimiter ;
    
    call transport_status("2014-09-01 00:00:00","2014-09-2 00:00:00");
    



  • 相关阅读:
    android-exploitme(六):基础加密
    错误:error libGL.so: cannot open shared object file: No such file or directory
    android-exploitme(五):不安全的数据存储
    android-exploitme(四):参数篡改
    android-exploitme(三):安全连接
    android-exploitme(二):安装apk熟悉测试环境
    android-exploitme(一):生成apk
    Ubuntu rsync同步
    phantomjs + selenium headless test
    Fatal error: cannot allocate memory for the buffer pool
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6690772.html
Copyright © 2011-2022 走看看