为不同表中的另一个字段选择SUM multiply字段,这等于

时间:2016-10-05 10:01:58

标签: mysql

我通过例子解释:

第一张表是产品:

id|price|
1   30

第二个表是仓库:

id|id_product|amount|sell_price|
1       1       1        50

我想要货币(仓库表)*价格(产品表)的总和,其中id(仓库表)= id(产品表)

$result_sum_ricavo = mysqli_query($link,"SELECT SUM(total_sum) FROM (p.prezzo * w.quantita as total_sum FROM prodotti p JOIN storico_magazzino w on p.id = w.id_collo AND w.causale='consegna' AND w.tipo='prodotto' AND w.importo_documento>0)");
$row_sum_ricavo = mysqli_fetch_assoc($result_sum_ricavo); 
$guadagno = $row_sum_ricavo["total_sum"];

我有这个错误:

  

mysqli_fetch_assoc()期望参数1为mysqli_result,boolean   在

中给出

1 个答案:

答案 0 :(得分:0)

SELECT
 SUM(total_sum)
FROM (
    SELECT p.price * w.amount as total sum
    FROM products p JOIN warehouse w on p.id = w.id
)