SELECT .. INTO returns more than one rows - Mysql

2020-05-07 03:24发布

问题:

i have a basic stored procedure

DECLARE user_o VARCHAR(50);

SELECT user_name INTO user_o FROM users WHERE topic_id = 54 AND entry_time BETWEEN 
2017-09-17 AND date_add( CURRENT_DATE, INTERVAL 1 DAY) ORDER BY entry_time ASC 
LIMIT 10;

this throws me error #1172 sql returns more than one rows. no idea why?

my goal is to have this result set

user_name |  user_o
  mike       mike
  liz        liz
  helen      helen
  her        her

回答1:

Its because you are inserting the result 'INTO' a variable and because the result is more than a single value, hence the error.

You have a limit of 10, try changing to 1, that will fix it, if you want multiple values returned then you need to manage these in a recordset.