mysql儲存過程示例
delimiter $$
use`mall_system`$$
//刪除儲存過程
drop
procedure
ifexists
`get_statistics`$$
//建立儲存過程
create
definer
=`root`
@`localhost`
procedure
`get_statistics`
(in shopid bigint
)//傳入的引數
begin
//定義變數
/*今日資料*/
declare today_money decimal(12
,2);
/*成交總額*/
declare today_visitor int
;/*訪客數*/
declare today_order int
;/*訂單數*/
declare total_user int
;/*累計客戶數*/
declare pay_consumer_number int
;/*支付人數*/
declare browse_number int
;/*瀏覽量*/
declare use_date int
;/*使用天數*/
/*昨日資料*/
declare yesterday_money decimal(12
,2);
/*成交總額*/
declare yesterday_visitor int
;/*訪客數*/
declare yesterday_order int
;/*訂單數*/
declare yesterday_pay_consumer int
;/*支付人數*/
declare yesterday_browse_number int
;/*瀏覽量*/
declare wait_to_deliver int
;/*待發貨*/
declare wait_to_pay int
;/*待付款*/
declare sku_low int
;/*查詢庫存過少商品數量*/
/*今日查詢*/
/*成交總額*/
select ifnull(
sum(total_real_price),0
)into today_money from
`shop_order`
where shop_id = shopid and
statusin(
1,2,
3)and datediff(curdate(
),pay_time)=0
;/*訪客數*/
select
count(*
)into today_visitor from
`visitor_records`
where shop_id = shopid and datediff(curdate(),
`date`)
=0;/*訂單數*/
select
count(*
)into today_order from
`shop_order`
where shop_id = shopid and
statusin(
1,2,
3)and datediff(curdate(
),pay_time)=0
;/*累計客戶數*/
select
count(*
)into total_user from
`user_info`
where shop_id = shopid;
/*支付人數*/
select ifnull(
count
(distinct
(user_id)),
0)into pay_consumer_number from
`shop_order`
where shop_id = shopid and datediff(curdate(
),pay_time)=0
;/*瀏覽量*/
select ifnull(
sum(times),0
)into browse_number from
`visitor_records`
where shop_id = shopid and datediff(curdate(),
`date`)
=0;/*使用天數*/
select datediff(curdate(
),create_time)
into use_date from
`shop`
where id = shopid;
/*昨日查詢*/
/*成交總額*/
select ifnull(
sum(total_real_price),0
)into yesterday_money from
`shop_order`
where shop_id = shopid and
statusin(
1,2,
3)and datediff(curdate(
),pay_time)=1
;/*訪客數*/
select
count(*
)into yesterday_visitor from
`visitor_records`
where shop_id = shopid and datediff(curdate(),
`date`)
=1;/*訂單數*/
select
count(*
)into yesterday_order from
`shop_order`
where shop_id = shopid and
statusin(
1,2,
3)and datediff(curdate(
),pay_time)=1
;/*支付人數*/
select ifnull(
count
(distinct
(user_id)),
0)into yesterday_pay_consumer from
`shop_order`
where shop_id = shopid and datediff(curdate(
),pay_time)=1
;/*瀏覽量*/
select ifnull(
sum(times),0
)into yesterday_browse_number from
`visitor_records`
where shop_id = shopid and datediff(curdate(),
`date`)
=1;/*待發貨*/
select
count(*
)into wait_to_deliver from
`shop_order`
where shop_id = shopid and
status=1
;/*待退款*/
select
count(*
)into wait_to_pay from
`shop_order`
where shop_id = shopid and(`
status`=
5or`status`=
4);/*查詢庫存過少商品數量*/
select
count(*
)into sku_low from
`prod_spu`
where shop_id = shopid and total_inventory <
10and putaway =
1and isdelete =0;
//返回查詢的結果
select today_money as todaymoney ,today_visitor as todayvisitor,today_order as todayorder,total_user as totaluser,
yesterday_money as yesterdaymoney,yesterday_visitor as yesterdayvisitor,yesterday_order as yesterdayorder,
wait_to_deliver as waittodeliver,wait_to_pay as waittopay,sku_low as skulow,pay_consumer_number as payconsumernumber,
browse_number as browsenumber,yesterday_pay_consumer as yesterdaypayconsumer,yesterday_browse_number as yesterdaybrowsenumber,
use_date as usedate;
end$$
delimiter
;
mysql儲存過程示例
create procedure pro cancel order in orderid int in userid int out resultstatus int comment 取消商品訂單 begin 引數說明 orderid 訂單id extras 擴充套件記錄 異常處理 declare ...
MySQL儲存過程示例
mysql儲存過程 自定義結束符 delimiter 如果存在同名的儲存過程就刪除 drop procedure if exists praddblack 建立儲存過程 create procedure praddblack in n int begin while n 999 do insert ...
mysql 儲存過程示例
在mysql的test庫中執行如下sql table structure for user drop table if exists user create table user id int 11 unsigned not null auto increment,tinyint 1 not nul...