为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Java MySQL

业务场景说明

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

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

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

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

表结构如图:

650

数据准备:

650

要解决的问题

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

解决思路:

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

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

实现步骤如下:

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

1、连接数据库

mysql -uroot -hlocalhost -p123456 -P3306

650

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

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 ;

650

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

4、调用存储过程

call order_state(10,20);

650

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

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 ;

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



作者:菩提狗
链接:https://www.jianshu.com/p/9351207ce435


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消