获取乘以两列然后SUM的总销售额

时间:2014-12-04 14:20:18

标签: php postgresql

我试图在PHP和Postgresql中将2列的SUM相乘。我似乎得到一个空洞的结果。

quantity和accessory_price位于一个名为accessory_only_solds的表中

这是非工作代码:

$aval = "SELECT TO_CHAR(SUM(CAST(accessory_price AS NUMERIC * quantity)),'999,999.99') AS atotal FROM accessory_only_solds";
$avalue = pg_query($aval);
$arev = pg_fetch_assoc($avalue);
echo $arev['atotal'];

如果我删除数量我得到的accessory_price的SUM不正确得到总数,因为一些配件超过1个数量

使用accessory_price的SUM,但没有乘以数量:

    $aval = "SELECT TO_CHAR(SUM(CAST(accessory_price AS NUMERIC)),'999,999.99') AS atotal FROM accessory_only_solds";
    $avalue = pg_query($aval);
    $arev = pg_fetch_assoc($avalue);
    echo $arev['atotal'];

提前感谢您的帮助。

table

1 个答案:

答案 0 :(得分:0)

看起来你的演员阵容中存在乘法问题,这会导致问题。

试试这个:

SELECT TO_CHAR(SUM(CAST(accessory_price AS NUMERIC )* quantity),'999,999.99') AS atotal FROM accessory_only_solds