如何获得每月MYSQL查询的收益

时间:2017-05-15 20:16:43

标签: mysql

我有两个表,一个名为“tbl_materiales”的表,另一个名为“tbl_pedidos”。在名为tbl_materiales的表中“我有关于我所有产品的信息,如描述,以及最重要的”价格“......

在我的“tbl_pedidos”表中,我注册了用户在网站上注册的产品的所有信息。

例如:

tbl_materiales:

IdProduct     Description   Price
   5        Product one     8
   6        Product three   10
   7        Product four    15

tbl_pedidos

IdProduct   Quantity    Month 
   5        10          January
   6        5           January
   7        2           February

所以,我想知道每个月的所有收益...... 我希望这样:列收益是tbl_pedidos.Quantity * tbl_materiales.Price的乘积,显然取决于产品的价格和售罄的数量。

Month       Earnings
January          130
February          30

现在,我有这个,但它没有给我带来正确的信息......

SELECT tbl_pedidos.Mes
     , (SUM(tbl_pedidos.Cantidad) * tbl_materiales.Precio) as Total 
FROM tbl_pedidos 
INNER JOIN tbl_materiales 
  ON tbl_pedidos.IdMaterial = tbl_materiales.IdMaterial 
GROUP BY tbl_pedidos.Mes 
ORDER BY tbl_pedidos.Fecha;

3 个答案:

答案 0 :(得分:0)

查询可以是:

SELECT tbl_p.Month
     ,sum(as tbl_m.Price*TP.Quantity) AS Earnings 
FROM tbl_materiales AS tbl_m 
JOIN tbl_pedidos AS tbl_p
  ON tbl_m.IdProduct = tbl_p.IdProduct
 GROUP 
    BY tbl_p.Month;

答案 1 :(得分:0)

SELECT tbl_pedidos.Mes , SUM(tbl_pedidos.Cantidad*tbl_materiales.Precio) as Total 
FROM tbl_pedidos 
INNER JOIN tbl_materiales 
  ON tbl_pedidos.IdMaterial = tbl_materiales.IdMaterial 
GROUP BY tbl_pedidos.Mes 
ORDER BY tbl_pedidos.Fecha;

检查http://sqlfiddle.com/#!9/de665b/1

答案 2 :(得分:-1)

在这种情况下,我使用Where而不是Join,也许de next句子可以解决你的问题:

select TP.Month,sum(TM.Price*TP.Quantity) as Earnings 
from TBL_Pedidos TBP,TBL_Materiales TM 
where TP.IdProduct = TM.Id_Product 
group by TP.Month

分组依据是解决方案