如何将SELECT语句中的值存储在MySQL存储过程中的变量中?

时间:2022-10-07 16:38:19

This may be a super easy question to answer, but I am unsure how to do this correctly.

这可能是一个非常容易回答的问题,但我不确定如何正确地做到这一点。

Here is my query within the procedure:

这是我在程序中的查询:

SELECT COUNT(barcode) AS count FROM movieitems;

How do I store the return value of this statement (for example, the value in count is 5) into a variable? I want to be able to access the count value throughout the rest of my procedure.

如何将此语句的返回值(例如,count中的值为5)存储到变量中?我希望能够在我的其余程序中访问计数值。

2 个解决方案

#1


16  

In a stored procedure do this:

在存储过程中执行以下操作:

SELECT COUNT(barcode) AS count into @myVar FROM movieitems;

#2


4  

SELECT @someVariable := COUNT(barcode) FROM movie ...

You can then use @someVariable in other queries. E.g.

然后,您可以在其他查​​询中使用@someVariable。例如。

SELECT * FROM some_table WHERE some_field > @someVariable;

And you can also manipulate the variable using SET:

您还可以使用SET操作变量:

SET @someVariable = @someVariable + 1;

#1


16  

In a stored procedure do this:

在存储过程中执行以下操作:

SELECT COUNT(barcode) AS count into @myVar FROM movieitems;

#2


4  

SELECT @someVariable := COUNT(barcode) FROM movie ...

You can then use @someVariable in other queries. E.g.

然后,您可以在其他查​​询中使用@someVariable。例如。

SELECT * FROM some_table WHERE some_field > @someVariable;

And you can also manipulate the variable using SET:

您还可以使用SET操作变量:

SET @someVariable = @someVariable + 1;