简体   繁体   中英

How get earnings per month MYSQL Query

I have two tables, one table called "tbl_materiales", and another called "tbl_pedidos".. In the table called tbl_materiales" I have information about all my products, Like Description, and the most important "Price"...

In my table "tbl_pedidos", i register all information of products that the user register in the website.

For example:

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

So, I want to know all the earnings PER month... I want to have this: The column earnings is the multiplication of tbl_pedidos.Quantity * tbl_materiales.Price, obviously it depends of the price of the product, and the quantity sold out.

Month       Earnings
January          130
February          30

Now, I have this, but it doesn't bring me the correct information...

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;

The query can be like :

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;
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;

Check http://sqlfiddle.com/#!9/de665b/1

In this case I have used Where instead of Join, maybe de next sentence resolve your problem:

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

Group by is the solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM