mysql之SELECT MAX(...) 在存储过程中错误地返回 NULL

EasonJim 阅读:63 2024-02-27 23:08:18 评论:0

我有一个表 ga_sum_1,其中有一列 created_timestamp。当我从 mysql 命令行执行以下查询时:

mysql> select max(created_timestamp) from ga_sum_1; 
+------------------------+ 
| max(created_timestamp) | 
+------------------------+ 
| 2017-11-05 00:59:55    | 
+------------------------+ 
1 row in set (0.00 sec) 

如果我从一个非常简单的存储过程中做同样的事情:

delimiter // 
create procedure test() 
begin 
  select max(created_timestamp) from ga_sum_1; 
end// 
delimiter ; 
 
mysql> call test(); 
+------------------------+ 
| max(created_timestamp) | 
+------------------------+ 
| 2017-11-05 00:59:55    | 
+------------------------+ 
1 row in set (0.00 sec) 

但是,当我在存储过程中这样做时:

drop procedure if exists test; 
 
delimiter // 
 
create procedure test() 
begin 
  declare cursor_end condition for sqlstate '02000'; 
  declare finished int default 0; 
  declare game_id int(11); 
  declare user_id int(11); 
  declare game_instance_id bigint(20); 
  declare currency varchar(15); 
  declare created_timestamp timestamp; 
  declare wager decimal(18,2); 
  declare win decimal(18,2); 
  -- cursor 
  declare game_action_csr cursor for select * from game_action_view; 
  declare continue handler for cursor_end set finished=1; 
 
  -- create view dynamically 
select max(created_timestamp) from ga_sum_1; 
 
end// 
 
delimiter ; 
 
mysql> call test(); 
+------------------------+ 
| max(created_timestamp) | 
+------------------------+ 
| NULL                   | 
+------------------------+ 
1 row in set (0.00 sec) 
 
Query OK, 0 rows affected (0.00 sec) 

我在最后一个例子中做错了什么?

====编辑====

一些进一步的研究表明它是 declare created_timestamp - 显然混淆了同名列上的选择!

请您参考如下方法:

这可能是因为这行:

  declare created_timestamp timestamp; 


标签:mysql
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号