chateaubriand chateaubriand - 1 month ago 9
MySQL Question

Join two tables (with calculation)

let's say I have this two tables in MySQL:

1. table_name



name
------
Max
John
Sarah
Peter


2. table_money



name | amount | month
-------------------------------
Max | 100 | January
Sarah | 90 | January
Max | 100 | February
Sarah | 90 | February
Peter | 90 | February


How has the SQL query to look, so that I can get a table like this:

name | total (amount * #month)
-------------------------------
Max | 200
Sarah | 180
Peter | 90


Thank you

Answer

Try using group by keyword

SELECT table_name.name,sum(table_money.amount) as total
FROM table_name JOIN table_money
on table_name.name = table_money.name
GROUP BY table_money.name

Hope this works