I have this table in which the last two rows are month and year respectively
where idalbaran is equivalent to an order number. Now, in another table I have the details of these orders, the table looks like this:
the last two rows are price and quantity
using mysql and php I need to obtain a list that looks like this:
This is the MySQL part:
SELECT
idcliente,
mes AS month,
anio AS year,
SUM(precio*candidad) AS totalSoldToThatClientThatMonth
FROM client_table CT
JOIN orders_table OT
ON CT.idalbaran = OT.idalbaran
GROUP BY idcliente, mes, anio
I assume the first table's name is client_table
, and the second name is orders_table
Try this:
select ft.idcliente, ft.mes, ft.anio,
sum(st.precio*st.cantidad) as Total
from firsTable ft
inner join secondTable st
on (st.idalbaram = ft.idalbaran)
group by ft.idcliente, ft.mes,ft.anio