mysql过程没有正常运行

anhgbhbe  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(371)

我有更新一些信息的程序。在table上,但是。它正在做。有些人认为这是完全错误的。正在更新。我的。表

CREATE DEFINER=`andrey.`@`ip` PROCEDURE `sp_visits`()
BEGIN

  DECLARE cursor_VAL VARCHAR(255);
  DECLARE done INT DEFAULT FALSE;
  DECLARE min_date DATETIME DEFAULT FALSE;
  DECLARE max_visits INT DEFAULT FALSE;
  DECLARE cursor_i CURSOR FOR SELECT hash_id FROM .ANDREY;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

  OPEN cursor_i;
  read_loop: LOOP
    FETCH cursor_i INTO  cursor_VAL;
    IF done THEN
      LEAVE read_loop;
    END IF;

   select @max_visits := max(visits)
    from ANDREY where hash_id = cursor_VAL ;

    select @min_date :=min(date(transaction_ts)) 
    from ANDREY where hash_id = cursor_VAL and visits =@max_visits;

   update ANDREYt 
   set visits = @max_visits+1
   where hash_id = cursor_VAL  and date(transaction_ts) between date(@min_date) and DATE(@min_date) + INTERVAL 7 DAY;

  END LOOP;
  CLOSE cursor_i;
END

当我从workbench运行它时,它将返回一些记录#@min_date:=min(date(transaction_ts))2018-07-20
我不明白为什么,因为我没有为这个过程指定return,也不明白为什么它不更新我的表,当我手动运行语句时,procedure logic中的语句不起作用,但procedure中的语句不起作用。有什么想法吗?
用这个光标,我试图解决这个问题

brqmpdu1

brqmpdu11#

当你做以下事情时:

select @max_visits := max(visits)
from ANDREY where hash_id = cursor_VAL ;

在过程中,此查询的结果作为过程的结果返回。你可以用 SELECT INTO :

SELECT MAX(visits) INTO @max_visits
FROM ANDREY where hash_id = cursor_VAL ;

SELECT MIN(DATE(transaction_ts) INTO @min_date
FROM ANDREY where hash_id = cursor_VAL AND visits = @max_visits;

相关问题