mysql存儲過程及日期函數實踐

業務場景說明

假設待審覈的訂單表有三種狀態,分別是“正常待審覈”|“即將過期”|“已經過期”

訂單提交後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 ;

**********書山有路,學海無涯,無數個孤獨的夜晚,需要一點小小的成就感!**********

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章