mysql 游標型別

2021-07-14 07:06:31 字數 4458 閱讀 3504

mysql裡面不可以返回游標,很讓人蛋疼,沒oracle好,而且,mysql沒有什麼好的除錯工具,也比較糾結.

下面是我寫的乙個mysql儲存過程,裡面使用到了游標,臨時表,最終返回的是結果接,希望對大家有幫助:

delimiter $$

use `realwar`$$

drop procedure if exists `queryremainspoints`$$

create definer=`root`@`%` procedure `queryremainspoints`(

/*in startsize int,

in pagesize int,startsize,分頁的起始位置,pagesize 分頁的大小,

in queryusername varchar(100),/*queryusername根據使用者名字來查詢,

in remainp int,

in remainp1 int,/*remainp 使用者剩餘軍餉小,remainp1剩餘軍餉大

in sump int,

in sump1 int, payp 使用者充值總額小,payp 總金額大

in rats int ,

in rats1 int滯留率查詢*/

)begin

declare usernames varchar(50);

declare nickname varchar(50);

declare sumpoints int;

declare remainpoint int;

declare daypay int default 0;

declare daycost int;

declare sumcost int; 

declare dayremainrats varchar(50);

declare sumremainrats varchar(50);

declare    over int default 0;

/*定義乙個游標,獲得所有的使用者*/

/*if(queryusername is not null)else{

declare usernamecur cursor for select rp.username as username from realwar_tool.play_money_pay_log rp group by (rp.username);

declare continue handler for sqlstate '02000'set username = null;*/

declare usernamecur cursor for select rp.username  from realwar_tool.play_money_pay_log rp group by (rp.username);

declare continue handler for sqlstate '02000'set usernames = null;

/*建立乙個臨時表,用來存放資料*/

drop  table if exists realwar.temp_remainspoints;

create temporary table realwar.temp_remainspoints(

sid int(10) primary key auto_increment,

username1 varchar(100),

nickname1 varchar(100),

sumpoints1 int(10),

remainpoints1 int(10),

daypay1  int(10),

daycost1 int(10),

dayremainrats1 varchar(10),

sumremainrats1 varchar(10)

);open usernamecur;/*開啟游標*/

/*usernamecur:loop */

fetch usernamecur into usernames;/*迴圈游標,給username賦值*/

while(usernames is not null) do 

/*玩家暱稱*/

select (rp.nickname) into nickname  from realwar.playbaseinfo rp where rp.username = usernames;

/*今日充值軍餉*/

select sum(r1.change_money)*10 into daypay from realwar_tool.play_money_pay_log r1 where r1.username=usernames and year(from_unixtime(r1.dateline)) = year(sysdate()) 

and month(from_unixtime(r1.dateline)) = month(sysdate()) and date(from_unixtime(r1.dateline)) = date(sysdate());

if(daypay  is null) then

select 0 into daypay from dual;

end if;

/*使用者總充值軍餉或消費券*/

select sum(r1.change_money*10) into sumpoints from realwar_tool.play_money_pay_log  r1 where r1.username = usernames;

if (sumpoints is null) then 

select 0 into sumpoints from dual;

end if;

/*今日花費軍餉或消費券*/

select sum(t1.price) into daycost  from realwar.t_consumption t1 where t1.username = usernames  and year(str_to_date(t1.consumdate,'%y-%m-%d %h:%i:%s')) = year(sysdate()) 

and month(str_to_date(t1.consumdate,'%y-%m-%d %h:%i:%s')) = month(sysdate()) and date(str_to_date(t1.consumdate,'%y-%m-%d %h:%i:%s')) = date(sysdate());

if (daycost is null) then 

select 0 into daycost from dual;

end if;

/*使用者總花費軍餉或消費券*/

select sum(t2.price) into sumcost from realwar.t_consumption t2 where t2.username = usernames;

if (sumcost is null) then 

select 0 into sumcost from dual;

end if;

/*今日滯留*/

if(daypay!=0) then

select floor((daypay-daycost)/daypay*100) into dayremainrats from dual;

else

select 0 into dayremainrats from dual;

end if;

/*總滯留*/

if(sumpoints!=0)then 

select floor((sumpoints-sumcost)/sumpoints*100) into sumremainrats from dual;

else

select 0 into sumremainrats from dual;

end if;

/*將得到的值,迴圈的插入到臨時表中*/

insert into temp_remainspoints set username1=usernames,nickname1= nickname,sumpoints1= sumpoints,

remainpoints1= (sumpoints-sumcost),daypay1 = daypay,daycost1= daycost,dayremainrats1 = dayremainrats,sumremainrats1= sumremainrats;

fetch usernamecur into usernames;/*迴圈游標,給username賦值*/

end while;

/*end loop;*/

close usernamecur;

/*返回的結果集*/

select username1,nickname1,sumpoints1,remainpoints1,daypay1,daycost1,dayremainrats1,sumremainrats1 

from temp_remainspoints; 

end$$

delimiter ;

mysql游標型別 MySQL 游標

drop procedure if exists processorders create procedure processorders begin declare done boolean default 0 declare a int declare b varchar 20 定義游標遍歷時,...

mysql 游標 時間型別 mysql 游標型別

mysql裡面不可以返回游標,很讓人蛋疼,沒oracle好,而且,mysql沒有什麼好的除錯工具,也比較糾結.下面是我寫的乙個mysql儲存過程,裡面使用到了游標,臨時表,最終返回的是結果接,希望對大家有幫助 delimiter use realwar drop procedure if exist...

mysql游標的型別 MySQL 游標

以下的文章主要介紹的是mysql游標的使用筆記,其可以用在儲存過程的sql語句,其主要型別主要有以下幾種,以下就是對其詳細介紹,相信如果你掌握了這項技術,會在以後的學習或是工作中帶來很大的幫助。1 無返回結果語句,如 insert,update,drop,delete等 2 select語句返回單行...