美文网首页程序员
mysql存储过程及日期函数实践

mysql存储过程及日期函数实践

作者: 菩提狗 | 来源:发表于2018-07-05 01:42 被阅读0次

业务场景说明

假设待审核的订单表有三种状态,分别是“正常待审核”|“即将过期”|“已经过期”

订单提交后10小时内未处理:正常待审核

订单提交后10-20小时之间未处理:即将过期

订单提交后20小时后未处理:已经过期

表结构如图:

数据准备:

要解决的问题

校验订单表中不同状态订单的数量与前台显示是否一致

解决思路:

1、通过存储过程,获取数据库中的不同时间段的待审核订单数量

2、将后台获取的数据与前台进行比较

实现步骤如下:

=============================================================================

1、连接数据库

mysql -uroot -hlocalhost -p123456 -P3306

=============================================================================

2、进入数据库

use sqltestdb;

=============================================================================

3、创建存储过程(直接复制到mysql命令行执行就可以了)

DELIMITER //

CREATE PROCEDURE order_state(IN will_timeout int,IN already_timeout int)

BEGIN

declare count_normal int;

declare count_will int;

declare count_already int;

set @current_time=now();

set @time1=date_sub(@current_time, interval will_timeout hour);

select count(*) into count_normal from lijuan where order_time>@time1 and new_db=0;

select count_normal;

set @current_time=now();

set @time2=date_sub(@current_time, interval already_timeout hour);

select count(*) into count_will from lijuan where order_time>@time2 and new_db=0;

set count_will=count_will-count_normal;

select count_will;

select count(*) into count_already from lijuan where new_db=0;

set count_already=count_already-(count_will+count_normal);

select count_already;

END //

DELIMITER ;

==============================================================================

4、调用存储过程

call order_state(10,20);

==============================================================================

5、删除存储过程

drop procedure order_state;

==============================================================================

存储过程代码解释说明:

1-1声明结束符为“//”

DELIMITER //

1-2声明存储过程名称及输入参数的数量和类型:

存储过程名称:order_state

输入参数的数量:2

IN:表示是入参

will_timeout:即将超时所用的时间(例:10小时)

already_timeout:已经超时所用的时间(例:20小时)

CREATE PROCEDURE order_state(IN will_timeout int,IN already_timeout int)

1-3表示开始

BEGIN

1-4声明变量count_normal表示“正常”状态的订单的数量

declare count_normal int;

1-5声明变量count_will表示“即将超时”状态的订单的数量

declare count_will int;

1-6声明变量count_already表示“已经超时”状态的订单的数量

declare count_already int;

1-7获取当前时间并赋值给变量@current_time

set @current_time=now();

1-8将当前时间减去“即将超时所用的时间”,然后再赋值给变量@time1

set @time1=date_sub(@current_time, interval will_timeout hour);

1-9查询出“正常”状态的订单数量,并赋值给变量count_normal

select count(*) into count_normal from lijuan where order_time>@time1 and new_db=0;

1-10输出“正常”状态的订单数量

select count_normal;

1-11将当前时间减去“已经超时所用的时间”,然后再赋值给变量@time2

set @time2=date_sub(@current_time, interval already_timeout hour);

1-12查询出“正常”状态+“即将超时”状态的订单数量之和,并赋值给变量count_will

select count(*) into count_will from lijuan where order_time>@time2 and new_db=0;

1-13将“正常”状态+“即将超时”状态的订单数量之和(即count_will)减去“正常”状态的订单数量(即count_normal),然后再赋值给count_will

set count_will=count_will-count_normal;

1-14输出“即将超时”状态的订单数量

select count_will;

1-15查询出所有状态的订单数量(通过new_db=0过滤旧数据库的数据)

select count(*) into count_invalid from lijuan where new_db=0;

1-16将所有订单的数量减去“正常”状态+“即将超时”状态的订单数量之和(即count_will+count_normal)

set count_invalid=count_already-(count_will+count_normal);

1-17输出“已经超时”状态的订单数量

select count_already;

1-18结束

END //

1-19将结束符修改为“;”

DELIMITER ;

**********书山有路,学海无涯,无数个孤独的夜晚,需要一点小小的成就感!**********

相关文章

  • mysql存储过程及日期函数实践

    业务场景说明: 假设待审核的订单表有三种状态,分别是“正常待审核”|“即将过期”|“已经过期” 订单提交后10小时...

  • MySQL 视图和存储程序

    MySQL 视图和存储程序 存储程序:存储函数、存储过程、触发器和事件的总称。 存储例程:存储函数+存储过程。 触...

  • 深入浅出MySQL之开发篇(二)

    继续深入了解MySQL的高级特性。 1.存储过程和函数 什么是存储过程和函数 存储过程和函数是事先经过编译并存储在...

  • MySQL存储过程和存储函数

    一、存储过程 MySQL存储过程存储过程和存储函数参考文章 SQL语句需要先编译然后执行,而存储过程(Stored...

  • MySQL存储过程创建及调用方法

    MySQL存储过程是一个sql语句,那么我们如何创建呢,MySQL存储过程创建及修改,删除操作。 1,存储过程创建...

  • mysql修改definer方法--转载

    -- 函数、存储过程 select definer from mysql.proc; update mysql.p...

  • Mysql存储过程函数

    MySQL存储过程的基本函数 字符串处理时,第一个字符下标为1,转换大小写时 非英文字母保留原样, load_fi...

  • mysql存储过程、函数

    一组预编译好的sql语句集合,理解成批处理语句。 优点:提高代码的重用性简化操作减少编译次数并且和数据库服务器连接...

  • MySQL函数&存储过程

    上一篇文章中说到了定位慢sql,拿到了慢sql后,我们要怎么重现问题呢?那么就需要造数据。函数和存储过程就可以帮助...

  • MySQL常用的日期加减函数与实例教程

    MySQL 日期类型MySQL 日期类型:日期格式、所占存储空间、日期范围 比较。日期类型 存储空间...

网友评论

    本文标题:mysql存储过程及日期函数实践

    本文链接:https://www.haomeiwen.com/subject/udkuuftx.html