我的查询的目标是返回国家名称及其国家元首,如果它的headofstate名称以A开头,并且该国家的首都使用嵌套查询的人数超过100,000人.
这是我的查询:
SELECT country.name as country,
(SELECT country.headofstate
from country
where country.headofstate like 'A%')
from country, city
where city.population > 100000;
Run Code Online (Sandbox Code Playgroud)
我试过反转它,把它放在where子句等.我没有得到嵌套查询.我只是得到错误,比如"子查询返回多行"等等.如果有人可以帮助我如何订购,并解释为什么它需要以某种方式,这将是伟大的.
Erw*_*ter 17
如果必须"嵌套",这将是一种完成工作的方法:
SELECT o.name AS country, o.headofstate
FROM country o
WHERE o.headofstate like 'A%'
AND (
SELECT i.population
FROM city i
WHERE i.id = o.capital
) > 100000
Run Code Online (Sandbox Code Playgroud)
但是,A JOIN
比相关子查询更有效.是不是,曾经给你这项任务的人不能自己加速?
您需要join
两个表,然后过滤结果in where
子句:
SELECT country.name as country, country.headofstate
from country
inner join city on city.id = country.capital
where city.population > 100000
and country.headofstate like 'A%'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
111766 次 |
最近记录: |