我知道在 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 中做到这一点,或者我需要一些扩展?提前致谢!
Schema
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');
Query
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 |
+-------+-------------+
像上面这样选择变量的重要部分是将结果集限制为最多 1 行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)