将查询结果设置为MySQL中的变量 [英] Set the result of a query to a variable in MySQL

查看:580
本文介绍了将查询结果设置为MySQL中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个简单的语法内容: 我正在尝试在MySQL中将变量设置为等于查询结果,例如:

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.

推荐答案

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

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

SELECT @variable1;

这篇关于将查询结果设置为MySQL中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆