亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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
提交
取消