为什么这不起作用,我试图得到以前和现在的值来计算百分比变化。这两个值我都得到了正确的结果,但是现在如何重用它们来进行数学运算呢
当我尝试下面的命令时
错误1054(42S22):“字段列表”中的未知列“currentVal”
SELECT IFNULL(DValue,0) as currentVal,
(SELECT IFNULL(DValue,0)
FROM ...
WHERE...) as previousVal,
(currentVal-previousVal)/previousVal
FROM ...
WHERE ...;
最佳答案
将另一个查询包装在您当前拥有的内容上并计算您在其中的百分比:
SELECT currentVal, previousVal,
(currentVal-previousVal)/previousVal AS percentChange
FROM (SELECT IFNULL(DValue,0) as currentVal,
(SELECT IFNULL(DValue,0)
FROM ...
WHERE...) as previousVal
FROM ...
WHERE ...) t