Search code examples
mysqlsqlstored-procedurespercona

How to set a variable as query result in a MySQL stored procudure?


A rather simple question, but I can only find answers to more complex questions.

I'm working on a stored procudure and am currently inside a REPEAT loop. I need to run the following query in the loop to get the 1 column value that is returned (only 1 record will be returned). This will need to be stored as a var to be used later in the loop.

SELECT photo_id FROM photos ORDER BY photo_id DESC LIMIT 1;

How do I set that to 'last_photo_id' to be used later in the stored procdure?


Solution

  • Try this:

     DECLARE total_products INT DEFAULT 0
     SELECT COUNT  INTO total_products
     FROM products