当类似的sql查询给出结果时,MySQL过程返回NULL

时间:2015-02-09 14:52:24

标签: mysql sql stored-procedures

当我使用查询时:

SELECT game_id FROM games_players WHERE player_id = 1 LIMIT 1 

我得到了一个结果:

game_id
-------
      1

但是当我将它作为存储过程运行时(当然是在同一个表上):

CREATE FUNCTION `start_game` (player_id_1 INT(11) UNSIGNED) 
    RETURNS INT(11) UNSIGNED NOT DETERMINISTIC
BEGIN
SELECT game_id INTO @game_id_1 FROM games_players WHERE player_id = 1 LIMIT 1;
RETURN @game_id_1;
END
END

有:

SET @p0='1';
SELECT `start_game`(@p0) AS `start_game`;

我得到了:

start_game
----------
      NULL

为什么程序没有返回1而是给我NULL

编辑:

以下任何作为我的程序正文的行为相同(大多数尝试都基于评论):

order by(当你限制结果时,这总是一件好事):

...
SELECT game_id INTO @game_id_1 FROM games_players WHERE player_id = 1 ORDER BY game_id LIMIT 1;
RETURN @game_id_1;
...

player_id_1而不是硬编码1(如果即使是硬编码1也不起作用,那么......):

...
SELECT game_id INTO @game_id_1 FROM games_players WHERE player_id = player_id_1 ORDER BY game_id LIMIT 1;
RETURN @game_id_1;
...
没有限制! (我知道结果只包含一个和一个值。)

...
SELECT game_id INTO @game_id_1 FROM games_players WHERE player_id = 1;
RETURN @game_id_1;
...

0 个答案:

没有答案