1

我有以下语句,它会产生此错误。显然,select 语句第一行中的别名“R”没有声明,但它就在语句的末尾。有人有想法么?

#1327 - 未声明的变量:R

    CREATE PROCEDURE `get_series_completion`(IN team1_id INT, IN team2_id INT, IN round_shortname VARCHAR(5))
BEGIN
    DECLARE num_games_total, num_games_played INT;

    SELECT COUNT(*) INTO num_games_played, R.games_per_series INTO num_games_total
    FROM (
        SELECT game_id
        FROM team_games
        WHERE team_id IN (team1_id, team2_id)
        GROUP BY game_id
            HAVING COUNT(*) = 2
            AND SUM(standing = 0) = 0
    ) AS S, rounds AS R ON R.round_shortname = round_shortname;

    SELECT num_games_played/num_games_total;
END$$
4

1 回答 1

1

您应该在 into 子句之前选择所有列,您的查询应该是:

SELECT COUNT(*), R.games_per_series INTO num_games_played, num_games_total
    FROM (
        SELECT game_id
        FROM team_games
        WHERE team_id IN (team1_id, team2_id)
        GROUP BY game_id
            HAVING COUNT(*) = 2
            AND SUM(standing = 0) = 0
    ) AS S 
    LEFT OUTER JOIN rounds AS R ON R.round_shortname = round_shortname;

或者

SELECT COUNT(*), R.games_per_series INTO num_games_played, num_games_total
    FROM (
        SELECT game_id
        FROM team_games
        WHERE team_id IN (team1_id, team2_id)
        GROUP BY game_id
            HAVING COUNT(*) = 2
            AND SUM(standing = 0) = 0
    ) AS S, rounds AS R 
    WHERE R.round_shortname(+) = round_shortname; -- implicit left outer join
于 2013-04-05T04:39:40.107 回答