Reputation: 1180
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:
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.
Upvotes: 35
Views: 113060
Reputation: 5622
SELECT @code:=salary FROM employee_info WHERE emp_id = 12345678;
To check salary,
SELECT @code;
The result of salary will be initialized in code
.
Upvotes: 2
Reputation: 77936
select @variable1 := salary FROM employee_info WHERE emp_id = 12345678;
Upvotes: 1
Reputation: 3333
Set the result of a query to a variable in MySQL
Select @Amount1:= Amount FROM table where id=57703;
Upvotes: 2
Reputation: 1325
use this
SELECT weight INTO @x FROM p_status where tcount=['value'] LIMIT 1;
tested and workes fine...
Upvotes: 1
Reputation: 131
You can even fill multiple variables in a single query.
SELECT salary, salary_group INTO @var1, @var2 FROM employee_info WHERE emp_id = 12345678;
Upvotes: 13
Reputation: 63105
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;
Upvotes: 74
Reputation: 453
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;
Upvotes: 4