0

想象一下下表用户

Users
------------------------

CITY        USERNAME        TOPIC 
city1        george          topic1
city1        george          topic2
city1        george          topic3
city1        chris           topic1
city1        chris           topic2
city2        john            topic1
city3        jenny           topic1

我想要按城市划分的以下结果组

city1 -> 2 users
city2 -> 1 user
city3 -> 1 user

任何帮助表示赞赏!

4

1 回答 1

3

如果您想要每个城市的不同用户:

select city, count(distinct username) TotalUsers
from users
group by city;

请参阅SQL Fiddle with Demo。结果是:

|  CITY | TOTALUSERS |
----------------------
| city1 |          2 |
| city2 |          1 |
| city3 |          1 |
于 2013-06-05T23:12:08.877 回答