简体   繁体   English

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

[英]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.

相关问题 如何设置一个变量来存储 select 语句返回的值并在存储过程 MySQL 中执行操作? - How to set a variable to store a value returned by select statement and perform an operation in stored procedure MySQL? MySQL存储过程从选择语句为多个变量赋值 - MySQL store procedure Assign value to multiple variable from select statement 如何将MySQL存储过程中的选定值存储到PHP中的变量中? - How to store a selected value from a MySQL stored procedure into a variable in PHP? 有没有办法将 MySQL 中存储过程中准备好的语句返回的值存储在变量中? - Is there a way to store in a variable the value returned from a prepared statement in a Stored Procedure in MySQL? 更新记录时,如何保持MySQL存储过程中的变量值不变? - How do I keep the variable value in a MySQL stored procedure from changing when records are updated? 如何将变量值传递给mysql中全文语句的存储过程? - How to pass variable value to stored procedure for fulltext statement in mysql? 如何在select语句后在mysql存储过程中插入值 - How can I insert value in mysql stored procedure after select statement 如何从表中获取特定值并将其存储在存储过程中的变量中 - how to get a particular value from table and store in variable in stored procedure mysql中的select语句存储过程 - select statement stored procedure in mysql MySQL存储过程变量格式在SELECT语句中不起作用 - MySQL Stored Procedure variable format not working in SELECT statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM