我了解 T-SQL,但对 MySQL 还很陌生。我真的很想做这样的事情,也许使用子查询,也许使用临时表,可能在例程中。每个选择的结果有多行,行数不同。
create procedure procname
select col1 into @var1 from t1 inner join t2 on t1.id = t2.id where t2.col2 = 1;
select col1 into @var2 from t1 inner join t2 on t1.id = t2.id where t2.col2 = 2;
select col1 into @var3 from t1 inner join t2 on t1.id = t2.id where t2.col2 = 3;
create temporary table tmpT (col1,col2,col3) insert (@var1, @var2, @var3);
select * from tmpT;
但是我尝试的一切都会引发错误。如果已经有人问过这个问题,我深表歉意,但我找不到任何人提出这个问题的帖子。哦,顺便说一句,我使用的是 phpmyadmin,mysql 版本 5.6.x
最佳答案
您可以将 MAX
与 CASE
语句结合使用:
select max(case when t2.col2 = 1 then col1 end) as col1,
max(case when t2.col2 = 2 then col1 end) as col2,
max(case when t2.col2 = 3 then col1 end) as col3
from t1 inner join t2 on t1.id = t2.id
where t2.col2 in (1,2,3)
关于mysql - 使用来自 1 个表的 1 列的变量的三个 MySQL 查询需要解析到临时表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25922403/