繁体   English   中英

在 select 查询中使用 Oracle

[英]Using Oracle in select query

问题:给定 CITY 和 COUNTRY 表,查询 CONTINENT 为“Asia”的所有城市的人口总和。

City 表包含字段:CountryCode,Population Country 表包含字段:Code,Continent(CITY.CountryCode 和 COUNTRY.Code 是匹配的键列。)

我尝试了以下查询:(我知道这可以使用内部连接来解决)

Select sum(city.population) from city
where city.countrycode in (Select code from Country where continent = 'Asia')

黑客等级给出以下错误:第 3 行错误:ORA-00933:SQL 命令未正确结束

你需要一个分号吗?

沿着这些路线的东西......

每个城市…………

SELECT City.Name, SUM(City.Population)
FROM City INNER JOIN Country ON Country.Code = City.CountryCode
WHERE
  Country.Continent = 'ASIA'
GROUP BY 
  City.Name;

每个国家和城市

SELECT Country.Name, City.Name, SUM(City.Population)
FROM City INNER JOIN Country ON Country.Code = City.CountryCode
WHERE
  Country.Continent = 'ASIA'
GROUP BY 
  Country.Name, City.Name;

只是亚洲的总数

SELECT SUM(City.Population)
FROM City INNER JOIN Country ON Country.Code = City.CountryCode
WHERE
  Country.Continent = 'ASIA';

我们在这里做的是查询 city 表并使用这两个表中的识别字段查找 country 表中的所有匹配项:

country.code在您的国家/地区表中

city.countrycode在您的城市表中

每当country.code = city.countrycodecountry.continent= 'Asia' ,您将得到该人口的总和。

Select sum(city.population) from city inner join country on country.code = city.countrycode
where country.continent= 'Asia';

我还建议您选择人口计数所属的城市:

Select city.name, sum(city.population) from city inner join country on country.code = city.countrycode
where country.continent= 'Asia';

这是正确的代码

Select sum(city.population) 
from city inner join country on country.code = city.countrycode
where country.continent= 'Asia';

选择 SUM(City.POPULATION) from City join Country ON city.countrycode=country.code where country.continent='Asia'

利用; 在代码的末尾。

Select sum(city.population) from city where city.countrycode in (Select code from Country where continent = 'Asia');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM