我有一张桌子:
ID | domain | address | downloads
====================================================
1 | example.com | example.com/siteA | 1
2 | example.com | example.com/siteB | 2
3 | example.com | sub.example.com | 1
4 | example.com | sub.example.com/dir | 2
5 | foobar.com | foobar.com/site123 | 1
6 | foobar.com | foobar.com/site | 2
...
现在我想知道一个域名有多少“下载”
domain | downloads
=======================
example.com | 6
foobar.com | 3
我的陈述目前是
SELECT domain, COUNT(downloads) as downloads FROM table GROUP BY domain
但这会(当然)返回
domain | downloads
=======================
example.com | 4
foobar.com | 2
请注意我不知道域名所以我不能使用条件!
答案 0 :(得分:3)
使用SUM
代替COUNT
:
SELECT domain, SUM(downloads) as downloads
FROM table
GROUP BY domain
答案 1 :(得分:1)
您可以使用以下总和
select domain,sum(downloads) from tableA group by domain