MySQL 带有光标的程序未 运行 正确

MySQL procedure wit cursor not running properly

我有程序需要更新一些信息。在 table 中,但是 .它在做什么。有些这完全错误而不是。更新。我的 。 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 它而不是 运行 宁它 return 我一些记录 # @min_date :=min(date( transaction_ts)) 2018-07-20

我不明白为什么,因为我没有为这个过程指定 return,也不明白为什么它不更新我的 table,当我 运行ning 手动声明时在程序逻辑中工作,但在程序中不起作用。有任何想法吗?

当您执行以下操作时:

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;