来自外部表的计数总和

时间:2013-07-18 19:11:40

标签: sql count sum

我需要搜索特定开发者制作的游戏总和。我有两张桌子:

 _____________________________
|____________GAMES____________|
| Id | Title | id_dev |  hits |
| 01 | abc   |    1   |   20  |
| 02 | xyz   |    2   |   15  |
| 03 | cde   |    1   |   9   |

 _______________
|__DEVELOPERS___|
| Id |    Title |
| 01 |    poi   |
| 02 |    asd   |
| 03 |    qwe   |

我希望结果格式化为Developers title 40,其中40是具有此开发者ID的游戏的所有点击的总和。我怎么能这样做呢?

SELECT developers.title, COUNT(count) AS total FROM (SELECT COUNT(games.hits) AS count
FROM games
GROUP BY id_dev
HAVING count > 1) as A
FROM developers
JOIN games
WHERE developers.id = games.id_dev

1 个答案:

答案 0 :(得分:4)

这是一个简单的连接和聚合,因此您过于复杂:

select d.id, d.title, sum(g.hits)
from games g join
     developers d
     on g.id_dev = d.id
group by d.id, d.title;