MySQL & PHP - 从数据库表中获取国家降序计数列表的最佳方式

我正在尝试编译(以在网页中显示)国家列表和 MySQL 数据库中的计数。这些条目在一个表中,我需要在一个页面上显示数据库中的国家列表,按照有多少条目的顺序。

所以我知道如何从数据库中获取国家列表,例如,

SELECT COUNT(*) FROM table_name WHERE country_code='GB'

我知道如何获取国家列表,例如,

SELECT DISTINCT country_code FROM table_name;

但我正在努力将它们放在一起并获得仅实际在数据库中的国家/地区的列表,然后是它们的计数,然后将它们按降序排列。

在正确的方向寻找一个温和的推动来找出如何做到这一点。


慕虎7371278
浏览 199回答 2
2回答

猛跑小猪

SELECTCOUNT(*),country_codeFROM table_nameGROUP BY country_codeGROUP BY是您解决此类问题所需要的。ORDER BY如有必要,您可以在查询末尾添加。

梦里花落0921

尝试:select count (*), country_code from (SELECT DISTINCT country_code FROM table_name)但正确的方法是使用GROUP BYselect count(*), country_code from table_namegroup by country_code要让他们按日期为 ex 排序,您可以:select count (*), country_code from (SELECT DISTINCT country_code  FROM table_name order by date)或者(通过使用分组方式):select count(*), country_code from table_namegroup by country_codeorder by date
打开App,查看更多内容
随时随地看视频慕课网APP