将查询结果设置为MySQL中的变量

时间:2021-12-03 15:42:47

This should be a simple syntax thing: I'm trying to set a variable in MySQL equal to the result of a query for instance:

这应该是一个简单的语法:我试图在MySQL中设置一个等于查询结果的变量:

SET @variable1 = SELECT salary FROM employee_info WHERE emp_id = 12345678;

Basically I want the salary from that employee to be stored as a variable that I can then manipulate and add.

基本上我希望将该员工的薪水存储为变量,然后我可以操作和添加。

What would the correct syntax for this be because I can't get it to work.

这是正确的语法是什么,因为我无法使它工作。

7 个解决方案

#1


55  

SELECT salary INTO @variable1 FROM employee_info WHERE emp_id = 12345678 LIMIT 1;

or

要么

SET @variable1 = (SELECT salary FROM employee_info WHERE emp_id = 12345678 LIMIT 1);

SELECT @variable1;

#2


12  

You can even fill multiple variables in a single query.

您甚至可以在单个查询中填充多个变量。

SELECT salary, salary_group INTO @var1, @var2 FROM employee_info WHERE emp_id = 12345678;

#3


3  

You are quite close to the right syntax. Here it is:

你非常接近正确的语法。这里是:

SET @variable1 = (SELECT salary FROM employee_info WHERE emp_id = 12345678);

and then print the variable like this:

然后打印变量,如下所示:

SELECT @variable1;

#4


2  

Set the result of a query to a variable in MySQL

将查询结果设置为MySQL中的变量

Select  @Amount1:=  Amount FROM table where id=57703;

#5


2  

SELECT @code:=salary FROM employee_info WHERE emp_id = 12345678;

To check salary,

要检查工资,

SELECT @code;

The result of salary will be initialized in code.

薪水的结果将在代码中初始化。

More Information

更多信息

#6


1  

use this

用这个

SELECT weight INTO @x FROM p_status where tcount=['value'] LIMIT 1;

tested and workes fine...

测试和工作正常...

#7


0  

select @variable1 := salary FROM employee_info WHERE emp_id = 12345678;

#1


55  

SELECT salary INTO @variable1 FROM employee_info WHERE emp_id = 12345678 LIMIT 1;

or

要么

SET @variable1 = (SELECT salary FROM employee_info WHERE emp_id = 12345678 LIMIT 1);

SELECT @variable1;

#2


12  

You can even fill multiple variables in a single query.

您甚至可以在单个查询中填充多个变量。

SELECT salary, salary_group INTO @var1, @var2 FROM employee_info WHERE emp_id = 12345678;

#3


3  

You are quite close to the right syntax. Here it is:

你非常接近正确的语法。这里是:

SET @variable1 = (SELECT salary FROM employee_info WHERE emp_id = 12345678);

and then print the variable like this:

然后打印变量,如下所示:

SELECT @variable1;

#4


2  

Set the result of a query to a variable in MySQL

将查询结果设置为MySQL中的变量

Select  @Amount1:=  Amount FROM table where id=57703;

#5


2  

SELECT @code:=salary FROM employee_info WHERE emp_id = 12345678;

To check salary,

要检查工资,

SELECT @code;

The result of salary will be initialized in code.

薪水的结果将在代码中初始化。

More Information

更多信息

#6


1  

use this

用这个

SELECT weight INTO @x FROM p_status where tcount=['value'] LIMIT 1;

tested and workes fine...

测试和工作正常...

#7


0  

select @variable1 := salary FROM employee_info WHERE emp_id = 12345678;