Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MySQL中创建/使用变量_Mysql_Variables - Fatal编程技术网

如何在MySQL中创建/使用变量

如何在MySQL中创建/使用变量,mysql,variables,Mysql,Variables,我知道,在MySQL中,您可以执行类似于从表t中选择foo作为bar的操作我试图做一些事情,效果是从表1中选择command1作为foo,从表2中选择command2作为bar,(foo-bar)作为差异

我知道,在MySQL中,您可以执行类似于从表t中选择foo作为bar的操作我试图做一些事情,效果是
从表1中选择command1作为foo,从表2中选择command2作为bar,(foo-bar)作为差异 模式
查询
视图变量

选择上述变量的重要部分是将结果集限制为最多1行。

foo-bar
将是一个select字段,它必须位于one from子句之前。你也应该加入表格。发布带有数据和预期输出的示例数据结构
create table t1
(   id int auto_increment primary key,
    thing varchar(100) not null
);

create table t2
(   id int auto_increment primary key,
    descr varchar(100) not null
);

insert t1(thing) values ('cat'),('dog');
insert t2(descr) values ('meow'),('likes bones');
select t1.thing,t2.descr into @var1,@var2 
from t1 
join t2 
on t2.id=t1.id 
where t1.id=2; -- to avoid Error 1172: Result consisted of more than one row
select @var1 as thing,@var2 as descr;
+-------+-------------+
| thing | descr       |
+-------+-------------+
| dog   | likes bones |
+-------+-------------+