加入两个表(带计算)

时间:2016-10-27 13:57:02

标签: mysql sql join

假设我在MySQL中有这两个表:

1。表名

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

如何查看SQL查询,以便我可以获得这样的表:

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

2 个答案:

答案 0 :(得分:2)

尝试按关键字分组

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

希望这有效

答案 1 :(得分:1)

Try this
SELECT name,sum(amount) Total 
FROM table_money 
GROUP BY name