将mysql值分配给变量内联 [英] assigning mysql value to variable inline

查看:108
本文介绍了将mysql值分配给变量内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不尝试这项工作,所以我试图获取之前和当前的值来计算百分比变化.我正确地获得了两个值,但是现在我该如何重用它们来进行数学运算

Why doesn't this work i'm trying to get the previous and current value to calculate percent change. I get both values correctly but now how can I reuse them to do the math operatio

当我尝试以下命令时,我得到 错误1054(42S22):字段列表"中的未知列"currentVal"

When I try the below command I get ERROR 1054 (42S22): Unknown column 'currentVal' in 'field list'

            SELECT IFNULL(DValue,0) as currentVal, 
                      (SELECT IFNULL(DValue,0) 
                       FROM ...
                       WHERE...) as previousVal, 
                      (currentVal-previousVal)/previousVal
            FROM ...
            WHERE ...;

推荐答案

将另一个查询围绕您当前拥有的内容并在那里计算百分比:

Wrap another query around what you currently have and calculate your percentage there:

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

这篇关于将mysql值分配给变量内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆