SQL 统计某个国家的用户数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3773351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:38:39  来源:igfitidea点击:

Count number of users from a certain country

sqlcount

提问by Danilo

I have a table of users, and in this table I have a country field telling where these people are from (i.e. "Sweden", "Italy", ...). How can I do a SQL query to get something like:

我有一个用户表,在这个表中我有一个国家字段,告诉这些人来自哪里(即“瑞典”、“意大利”……)。我怎样才能做一个 SQL 查询来得到类似的东西:

Country     Number
Sweden      10
Italy       50
...         ...

Users select their countries from a list I give to them, but the list is really huge so it would be great to have a SQL query that can avoid using that list, that is look in the DB and give back only those countries which are in the database, because for example I have nobody from Barbados, even if I have that option in the country select field of the signup form :)

用户从我给他们的列表中选择他们的国家,但这个列表真的很大,所以如果有一个 SQL 查询可以避免使用该列表,那就是在数据库中查找并只返回那些在数据库,因为例如我没有来自巴巴多斯的人,即使我在注册表单的国家/地区选择字段中有该选项:)

Thanks in advance!

提前致谢!

回答by Wolph

This will give what you want. But you might want to cache the result of the query. With a lot of users it's quite a heavy query.

这会给你想要的。但您可能希望缓存查询结果。对于很多用户来说,这是一个非常繁重的查询。

SELECT
    country,
    COUNT(*)
FROM
    users
GROUP BY
    country

Perhaps a better idea is (assuming you don't need the counts) to do it like this:

也许更好的主意是(假设您不需要计数)这样做:

SELECT
    DISTINCT country
FROM
    users

回答by Raj More

If the name of the country is in the Users table, try something like this:

如果国家名称在用户表中,请尝试以下操作:

SELECT Country, COUNT (*) AS Number
FROM Users
GROUP BY Country
ORDER BY Country

If the name of the country is in the country table, then you will have to join

如果国家/地区名称在国家/地区表中,则您必须加入

SELECT Contries.CountryName, Count (*) AS Number
FROM Users
INNER JOIN Countries
    ON Users.CountryId = Countries.CountryId
GROUP BY Countries.CountryName
ORDER BY Countries.CountryName

回答by AdaTheDev

Sounds like you want something like this...?

听起来你想要这样的东西......?

SELECT Country, COUNT(*) AS Number
FROM Users
GROUP BY Country

回答by marc_s

This is pretty straightforward:

这非常简单:

SELECT
    Country, COUNT(*) AS 'Number'
FROM
     YourTable
GROUP BY
    Country
ORDER BY
    Country

You just group your data by country and count the entries for each country.

您只需按国家/地区对数据进行分组并计算每个国家/地区的条目。

Or if you want them sorted by the number of visitors, use a different ORDER BY clause:

或者,如果您希望它们按访问者数量排序,请使用不同的 ORDER BY 子句:

SELECT
    Country, COUNT(*) AS 'Number'
FROM
     YourTable
GROUP BY
    Country
ORDER BY
    COUNT(*) DESC

回答by Alex Howansky

If you want the count per country:

如果您想要每个国家/地区的计数:

select country, count(*) from users group by country;

If you just want the possible values:

如果您只想要可能的值:

select distinct country from users;