假设我在MySQL中有这两个表:
name
------
Max
John
Sarah
Peter
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
答案 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