我可以: 邀请好友来看>>
ZOL星空(中国) > 技术星空(中国) > MySQL星空(中国) > 一个关于cursor 的疑问
帖子很冷清,卤煮很失落!求安慰
返回列表
签到
手机签到经验翻倍!
快来扫一扫!

一个关于cursor 的疑问

28浏览 / 0回复

109
精华
109
帖子

等  级:Lv.1
经  验:40
  • Z金豆:

    千万礼品等你来兑哦~快点击这里兑换吧~

  • 城  市:
  • 注  册:
  • 登  录:
发表于 2009-01-13 09:14:43
电梯直达 确定
楼主
MySQL 版本  5.1.30.以下是一个关于两个指针做循环调用的储存过程,该procedure是将符合条件的记录进行统计,并将结果插入到另一张表。(数据从表pts_log插入到表compcu)CREATE DEFINER = 'root'@'localhost' PROCEDURE `chatroom_compare`(IN sday DATE, IN eday DATE)
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGINdeclare stime,etime,chat_name,starttimes,endtimes VARCHAR(20);
declare no_more_rows ,a TINYINT(1) default 0;
declare shour,ehour TINYINT(2);
declare user_s,user_s2,durations smallint(6);declare cursor1 CURSOR for  
       select starttime, endtime,duration,TIME_FORMAT(starttime,"%H"),TIME_FORMAT(endtime,"%H")  from pts_log
       where srcusername = "kaixin001"  and action ='dis' and endtime between cast(date(sday) as datetime) and cast(date(eday) as datetime);

declare cursor2 CURSOR for
        select AA.chatname as chatname ,AA.users as users ,BBBB.users2 as user2,aa.starttimes as starttimes,aa.endtimes as endtimes
        from   
         (
          select   
          p.DESTUSERNAME as chatname,
          count(distinct(p.SRCUSERNAME)) as  users ,
          stime as starttimes,
          etime as endtimes
          from pts_log as p ,chatroom as cu
          where
          p.starttime  between stime and etime and
          p.action = "CLC" and
          p.DESTUSERNAME =  cu.username  AND
          CU.type = 1
          group by p.DESTUSERNAME
          ) as AA
         left join
         (
          select
          cu.username as chatname ,
          COUNT(DISTINCT(BB.srcusername)) as users2
          from chatroom as Cu,
           (
            select
            count(pts_log.srcusername) as times,
            pts_log.DESTUSERNAME as username,
            pts_log.SRCUSERNAME as srcusername,
            stime as startimes,
            etime as endtimes
            from pts_log
            where
            pts_log.action ="CLC" and
            pts_log.starttime between stime and etime
            group by pts_log.srcusername
            ) as BB
         where
         BB.times > 1 and
         cu.UserName = BB.username  AND
         CU.type =1
         group by cu.username
          ) as BBBB
        on AA.chatname=BBBB.chatname ; declare CONTINUE handler for not found  
       set no_more_rows = 1;

if EXISTS
   (
   SELECT starttime from pts_log where
   action = 'dis' and srcusername= 'kaixin001' and
   endtime between CAST(sday as datetime) and  CAST(eday as datetime)
    ) then
    set a =1;
end if ;
if  a =1 then
    open cursor1;
      LOOP1:loop  
       fetch cursor1 into stime ,etime,durations,shour,ehour ;  
          if no_more_rows THEN
          close cursor1;
          leave LOOP1;
          end if;
         
      if (durations > 600 &&( shour > 9 && ehour <21 ) ||  (shour >21) && durations < 10800  ) then
      
        open cursor2;
           LOOP2:loop  
             fetch cursor2 into chat_name,user_s, user_s2,starttimes,endtimes;
               if no_more_rows then
               close cursor2;
               leave LOOP2;
               end if;
      
               insert into compcu(chatname,users,users2,starttime,endtime) values(chat_name,user_s,user_s2,starttimes,endtimes);
           end loop LOOP2;
       end if ;
      
     end loop LOOP1;
   
end if;
END;在使用过程中,在compu表中发现第二指针仅仅插入了第一个指针中符合条件的第一条记录。现在还没有想到问题的答案。不知道有哪位达人可以帮鄙人解答,在下不胜感激。
高级模式
星空(中国)精选大家都在看24小时热帖7天热帖大家都在问最新回答

针对ZOL星空(中国)您有任何使用问题和建议 您可以 联系星空(中国)管理员查看帮助  或  给我提意见

快捷回复 APP下载 返回列表