找出GDP高于一组国家的国家

  • 本文关键字:国家 高于一 GDP 找出 sql
  • 更新时间 :
  • 英文 :


我正在SELECT教程中尝试SQLZoo的SELECT的问题5

哪些国家的GDP比欧洲任何国家都高?[只提供名称。]

这是我的解决方案,这是不正确的,但我不明白为什么。

SELECT name  
  FROM world  
 WHERE gdp > ALL (SELECT gdp FROM world WHERE continent ='Europe')

我做错了什么?

select name, gdp 
from world x 
where gdp > all (select gdp from world y where continent = "Europe" and gdp > 0)

您缺少NULL值检查,因为如果是NULL值,将获取未知结果,请参阅参考:http://dev.mysql.com/doc/refman/5.5/en/all-subqueries.html

当您测试查询时,一些欧洲国家的world表可能有null值,因此最好进行检查以避免null值。

SELECT name 
  FROM world 
 WHERE gdp > (SELECT MAX(gdp) FROM world WHERE continent = 'Europe')

使用MAX聚合函数从子查询中返回最高GDP,这是您希望与外部查询进行比较的结果。

select name from world 
 where gdp > ALL(select gdp from world 
 where continent = 'Europe' 
   and gdp is not null)

我连续地回答了这些问题,出于某种原因,我脑海中浮现的是上一个问题中的德国。所以我在我的问题中有这个,得到了正确的答案。哈哈,试试看。我用的是Chrome

从世界中选择名称其中gdp>全部(从世界看gdp其中name="德国"和人口<>0(

由于GDP有一个null值,所以如果将任何其他值与null进行比较,答案将为null。

SELECT name FROM world
WHERE
GDP > ALL(SELECT COALESCE(GDP,0) FROM world WHERE continent = 'Europe');

在上面,如果您传递COALESCE(GDP,0),则表示您正在传递GDP列,并且如果GDP中的任何值为Null,则COALESCE函数将返回"0"(用户提供的值(。

传递0作为比较的标准值,因为0是最小值。

试试这个:我们可以从所有选项中选择最大值,然后应用大于条件的

select name from world where gdp > (select max(gdp) from world where continent = 'Europe')
SELECT name FROM world
WHERE gdp >(SELECT MAX(gdp) FROM world WHERE continent = 'Europe' OR gdp > NULL);

您只需要使用OR语句,因为有些国家的gdp为NULL,这就是导致代码错误的原因。

试试这个:

select name 
from world
where gdp >= all(
    select gdp
    from world
    where 
    continent = 'Europe' and gdp is not null)
and continent <>'Europe'

试试这个:

SELECT name   FROM world  WHERE  gdp>= ALL(select gdp from world where continent='europe' and gdp>=0) and continent !='europe'
SELECT名称FROM世界其中GDP>全部(选择GDPFROM世界其中大陆="欧洲"和GDP>0(

空值问题

最新更新