MYSQL - SUM VALUE TWO TABLE

时间:2016-04-07 15:18:07

标签: php mysql sum

我需要使用别名在不同字段中的总和数量。

图片

enter image description here

2 个答案:

答案 0 :(得分:0)

我不知道你的两个表名是什么,但这应该有效。您在加入idMaterial后尝试在idMaterial上进行汇总。

SELECT A.idMaterial, C.nombre_material, A.sum_qty_entrada, B.sum_qty_salida, (A.sum_qty_entrada - B.sum_qty_salida) total 
FROM (
    SELECT idMaterial, SUM(qty) sum_qty_entrada FROM mesa_entrada
    GROUP BY idMaterial
)
    AS A 
JOIN (
    SELECT idMaterial, SUM(qty) sum_qty_salida FROM mesa_salida
    GROUP BY idMaterial
) AS B ON 
    A.idMaterial = B.idMaterial
JOIN mesa_con_nombre_material AS C ON
    A.idMaterial = C.idMaterial

答案 1 :(得分:0)

我认为你需要这个

SELECT SUM(hr) FROM
    (
      Select sum(qty) as hr FROM table1 
      UNION ALL
      Select sum(qty) as hr FROM table2 
    )a