本文介绍了如何在MySQL中制作/使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道在MySQL中您可以执行类似SELECT foo AS bar FROM TABLE t;
的操作,而我正在尝试执行SELECT command1 FROM table1 AS foo, command2 FROM table2 AS bar, (foo-bar) AS difference;
的操作,但是MySQL不允许我这样做.有没有办法在MySQL中做到这一点,或者我需要扩展一下吗?提前致谢!
I know that in MySQL you can do something like SELECT foo AS bar FROM TABLE t;
I'm trying to do something to the effect of SELECT command1 FROM table1 AS foo, command2 FROM table2 AS bar, (foo-bar) AS difference;
but MySQL doesn't let me do that. Is there a way to do this in MySQL or will I need to get some extension? Thanks in Advance!
推荐答案
架构
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
查看vars
select @var1 as thing,@var2 as descr;
+-------+-------------+
| thing | descr |
+-------+-------------+
| dog | likes bones |
+-------+-------------+
像上面一样选择var的重要部分是将结果集限制为最多1行.
The important part of selecting into vars like above is to limit the resultset to 1 row max.
这篇关于如何在MySQL中制作/使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!