简体   繁体   中英

MySQL - Result consisted of more than one row

I am a MySQL rookie and have been trying to create a stored procedure. The code below returns the error Error Code: 1172. Result consisted of more than one row . What am I doing wrong? (I'm using MySQL workbench)

CREATE DEFINER=`root`@`localhost` PROCEDURE `season_private_league_user`(
IN user_id INT,
OUT league_name VARCHAR(25),
OUT host_user VARCHAR(30))
BEGIN

    DECLARE userteamid INT;
    DECLARE var_league_name VARCHAR(25);
    DECLARE var_host_user VARCHAR(30);

    # Retrieve user team from user_id
    SELECT CS_USER_TEAMS_ID INTO userteamid
    FROM classicseasonmodel_classicseasonuserteam
    WHERE user_id = user_id;

    #LEAGUE NAME
    SELECT classicseasonmodel_classicseasonprivateleague.private_league_name INTO var_league_name
    FROM   classicseasonmodel_classicseasonuserteamprivateleague
    INNER JOIN classicseasonmodel_classicseasonprivateleague
    ON classicseasonmodel_classicseasonuserteamprivateleague.private_league_id=classicseasonmodel_classicseasonprivateleague.CS_PRIVATE_LEAGUE_ID
    WHERE  user_team_id = userteamid;

    #HOST_USER
    SELECT classicseasonmodel_classicseasonprivateleague.host_user_id INTO var_host_user
    FROM   classicseasonmodel_classicseasonuserteamprivateleague
    INNER JOIN classicseasonmodel_classicseasonprivateleague
    ON classicseasonmodel_classicseasonuserteamprivateleague.private_league_id=classicseasonmodel_classicseasonprivateleague.CS_PRIVATE_LEAGUE_ID
    WHERE  user_team_id = userteamid;

    SET league_name = var_league_name;
    SET host_user = var_host_user;

END

CALL season_private_league_user(2, @league_name, @host_user); SELECT @league_name AS league_name; SELECT @host_user AS host_user;

One of the SELECTs of you stored procedure that store the result in a variable returns more than one row, which returns in this error. This way you can only store single values in a variable, not multiple ones.

You can read about the SELECT...INTO statement here . The part that might be most interesting for you is:

The selected values are assigned to the variables. The number of variables must match the number of columns. The query should return a single row. If the query returns no rows, a warning with error code 1329 occurs (No data), and the variable values remain unchanged. If the query returns multiple rows, error 1172 occurs (Result consisted of more than one row). If it is possible that the statement may retrieve multiple rows, you can use LIMIT 1 to limit the result set to a single row.

Your column name and parameter name are identical. Rename your input parameter and change the command to this:

SELECT CS_USER_TEAMS_ID INTO userteamid
FROM classicseasonmodel_classicseasonuserteam
WHERE user_id = @user_id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM