获取相同行数
Getting the count of rows with the same
最近尝试学习一些东西 sql 并且遇到了这个问题。
我有一个页面显示国家名称及其在 div 中的首都,这是从我的数据库请求的。但是我想添加一个功能,用户可以根据给定国家包含的人口超过 100 万的城市数量来订购显示。但是,我想不出这里需要的查询。
如何获取同一国家/地区内人口大于 100 的城市的 数量 个整数值?我的 table 格式就是这样。因此,例如,英格兰应该 return 2 因为有两个英国城市满足条件,美国 3,印度 1 等等。所以美国将出现在我新订购的结果页面的顶部,其次是英格兰,然后是印度。
我试过联合查询,但无法获取第一个查询的值。
我该怎么做?
Table一个
*-----------------*------------*
| COUNTRY NAME | CAPITAL |
| England | London |
| India | New Delhi |
| US | DC |
| | |
*-----------------*------------*
Table B
*-----------------*------------------------------*
| COUNTRY NAME |Cities with a population > 1m |
| England | London |
| England | Birmingham |
| India | New Delhi |
| US | DC |
| US | New York |
| US | LA |
*-----------------*-------------------------------*
感谢您的帮助!
我假设 table B 中的所有值都是超过所需人口的城市。
如果不添加 where 子句来限制结果..
SELECT [COUNTRY NAME], COUNT([COUNTRY NAME])
FROM Table B
GROUP BY [COUNTRY NAME]
对于 Oracle 数据库,它将是:
select a.c_name, a.c_quan
from (select country_name c_name, count(country_name) c_quan
from table_b
group by country_name) a
order by a.c_quan;
SELECT country,
COUNT(*) AS NumCities
FROM countries
WHERE population > 100
GROUP BY country;
示例输出:
+---------+-----------+
| country | NumCities |
+---------+-----------+
| ad | 7 |
| ae | 3 |
| af | 48 |
| ag | 8 |
| ai | 1 |
| al | 66 |
| am | 287 |
| an | 13 |
| ao | 32 |
| ar | 84 |
| at | 221 |
...
| us | 4175 |
变体,这次检查 100 万:
SELECT country, COUNT(*) AS NumCities
FROM cities WHERE population > 1e6
GROUP BY country
ORDER BY NumCities DESC
LIMIT 11;
+---------+-----------+
| country | NumCities |
+---------+-----------+
| cn | 41 |
| in | 33 |
| br | 14 |
| ru | 12 |
| jp | 11 |
| id | 10 |
| mx | 9 |
| us | 9 |
| pk | 8 |
| ir | 7 |
| ng | 7 |
+---------+-----------+
11 rows in set (0.83 sec)
另一种变体:
SELECT country, city, FORMAT(population, 0)
FROM cities
WHERE population > 1000000
ORDER BY RAND()
LIMIT 11;
+---------+----------------+------------------------+
| country | city | FORMAT(population, 0) |
+---------+----------------+------------------------+
| in | Vadodara | 1,409,565 |
| in | Indore | 1,837,230 |
| am | Yerevan | 1,093,499 |
| br | Recife | 1,478,118 |
| mm | Mandalay | 1,208,227 |
| lb | Bayrut | 1,252,010 |
| ht | Port-au-Prince | 1,234,750 |
| ph | Manila | 10,443,877 |
| sa | Riyadh | 3,469,290 |
| pk | Faisalabad | 2,507,302 |
| cn | Lanzhou | 1,417,742 |
+---------+----------------+------------------------+
11 rows in set (0.98 sec)
最近尝试学习一些东西 sql 并且遇到了这个问题。 我有一个页面显示国家名称及其在 div 中的首都,这是从我的数据库请求的。但是我想添加一个功能,用户可以根据给定国家包含的人口超过 100 万的城市数量来订购显示。但是,我想不出这里需要的查询。
如何获取同一国家/地区内人口大于 100 的城市的 数量 个整数值?我的 table 格式就是这样。因此,例如,英格兰应该 return 2 因为有两个英国城市满足条件,美国 3,印度 1 等等。所以美国将出现在我新订购的结果页面的顶部,其次是英格兰,然后是印度。
我试过联合查询,但无法获取第一个查询的值。
我该怎么做?
Table一个
*-----------------*------------*
| COUNTRY NAME | CAPITAL |
| England | London |
| India | New Delhi |
| US | DC |
| | |
*-----------------*------------*
Table B
*-----------------*------------------------------*
| COUNTRY NAME |Cities with a population > 1m |
| England | London |
| England | Birmingham |
| India | New Delhi |
| US | DC |
| US | New York |
| US | LA |
*-----------------*-------------------------------*
感谢您的帮助!
我假设 table B 中的所有值都是超过所需人口的城市。 如果不添加 where 子句来限制结果..
SELECT [COUNTRY NAME], COUNT([COUNTRY NAME])
FROM Table B
GROUP BY [COUNTRY NAME]
对于 Oracle 数据库,它将是:
select a.c_name, a.c_quan
from (select country_name c_name, count(country_name) c_quan
from table_b
group by country_name) a
order by a.c_quan;
SELECT country,
COUNT(*) AS NumCities
FROM countries
WHERE population > 100
GROUP BY country;
示例输出:
+---------+-----------+
| country | NumCities |
+---------+-----------+
| ad | 7 |
| ae | 3 |
| af | 48 |
| ag | 8 |
| ai | 1 |
| al | 66 |
| am | 287 |
| an | 13 |
| ao | 32 |
| ar | 84 |
| at | 221 |
...
| us | 4175 |
变体,这次检查 100 万:
SELECT country, COUNT(*) AS NumCities
FROM cities WHERE population > 1e6
GROUP BY country
ORDER BY NumCities DESC
LIMIT 11;
+---------+-----------+
| country | NumCities |
+---------+-----------+
| cn | 41 |
| in | 33 |
| br | 14 |
| ru | 12 |
| jp | 11 |
| id | 10 |
| mx | 9 |
| us | 9 |
| pk | 8 |
| ir | 7 |
| ng | 7 |
+---------+-----------+
11 rows in set (0.83 sec)
另一种变体:
SELECT country, city, FORMAT(population, 0)
FROM cities
WHERE population > 1000000
ORDER BY RAND()
LIMIT 11;
+---------+----------------+------------------------+
| country | city | FORMAT(population, 0) |
+---------+----------------+------------------------+
| in | Vadodara | 1,409,565 |
| in | Indore | 1,837,230 |
| am | Yerevan | 1,093,499 |
| br | Recife | 1,478,118 |
| mm | Mandalay | 1,208,227 |
| lb | Bayrut | 1,252,010 |
| ht | Port-au-Prince | 1,234,750 |
| ph | Manila | 10,443,877 |
| sa | Riyadh | 3,469,290 |
| pk | Faisalabad | 2,507,302 |
| cn | Lanzhou | 1,417,742 |
+---------+----------------+------------------------+
11 rows in set (0.98 sec)