我想获取存储过程中的计算结果,该存储过程在示例中只允许一个参数(“代码”)。
delimiter $$
create procedure Test(in code varchar(8)) language sql deterministic
begin
set @sql=concat("select (a+b+c) as N, (d+e+f) as D from Tbl where code='",code,"'");
set r=N/D;
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
set @age=@NUM/@DEN;
end$$
我想要获得 N、D 和 r。
最佳答案
在我看来,使用 View 而不是存储过程可以更轻松地解决问题。例如,您可以定义 View
CREATE VIEW testview AS
SELECT *,N/D r FROM (select code, a+b+c N, d+e+f D FROM tmp) sub;
当您在SELECT
中使用它时,您可以轻松过滤您感兴趣的代码
,例如
SELECT * from testview WHERE code='yourcode'
然后你会得到类似的结果
code N D r
yourcode 468 234 2
该 View 当然可以与其他更复杂的查询或子查询结合使用。
编辑:
正如 Paul 所暗示的,如果在不使用子查询的情况下编写 View ,将会更加有效:
CREATE VIEW testview AS
SELECT code, a+b+c N, d+e+f D, (a+b+c)/(d+e+f) r FROM tmp;
关于mysql - MySQL 中的计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42958165/