[英]How do I store a value from a SELECT statement in a variable in a MySQL Stored Procedure?
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? 如何将此语句的返回值(例如,
count
值为5
)存储到变量中? I want to be able to access the count
value throughout the rest of my procedure. 我希望能够在我的其余程序中访问
count
数值。
在存储过程中执行以下操作:
SELECT COUNT(barcode) AS count into @myVar FROM movieitems;
SELECT @someVariable := COUNT(barcode) FROM movie ...
You can then use @someVariable
in other queries. 然后,您可以在其他查询中使用
@someVariable
。 Eg 例如
SELECT * FROM some_table WHERE some_field > @someVariable;
And you can also manipulate the variable using SET
: 您还可以使用
SET
操作变量:
SET @someVariable = @someVariable + 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.