Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 基于特定SQL组的列表_Mysql_Sql - Fatal编程技术网

Mysql 基于特定SQL组的列表

Mysql 基于特定SQL组的列表,mysql,sql,Mysql,Sql,我如何从下表中列出表面积大于同一地区所有其他国家的所有国家的名称: +----------------------------------------------+---------------------------+-------------+ | name | region | surfacearea | +---------------------------------

我如何从下表中列出表面积大于同一地区所有其他国家的所有国家的名称:

+----------------------------------------------+---------------------------+-------------+
| name                                         | region                    | surfacearea |
+----------------------------------------------+---------------------------+-------------+
| Aruba                                        | Caribbean                 |      193.00 |
| Afghanistan                                  | Southern and Central Asia |   652090.00 |
| Angola                                       | Central Africa            |  1246700.00 |
| Anguilla                                     | Caribbean                 |       96.00 |
| Albania                                      | Southern Europe           |    28748.00 |
| Andorra                                      | Southern Europe           |      468.00 |
| Netherlands Antilles                         | Caribbean                 |      800.00 |
到目前为止,我已经带来了这个代码,但这没有列出国家?这个代码正确吗

select region, max(surfacearea) as maxArea 
from country 
group by region; 

可能是,您可以使用一个内部连接和一个临时表

 select name from 
 country  as a
 inner  join      
 ( select region, max(surfacearea) as maxarea 
 from country 
 group by region ) as t    on a.region = t.region    
 where a.surfacearea =  t.maxarea;

看起来您的查询为每个区域标识了surfacearea的最大值。要获取国家,您可以再次将查询结果连接回国家表,以获取与region和surfacearea匹配的国家

 SELECT c.*
   FROM ( -- largest surfacearea for each region
          SELECT n.region
               , MAX(n.surfacearea) AS max_area
            FROM country n 
           GROUP BY n.region
        ) m
   JOIN country c
     ON c.region      = m.region 
    AND c.surfacearea = m.max_area

您可以在WHERE in contition中使用原始查询:

挑选* 来自农村 在哪一个地区,地表面积 选择区域,maxsurfacearea作为maxArea 来自农村 按区域分组 其他途径:

选择c1* 来自c1国 左联国家c2 关于c2.region=c1.region 和c2.surfaceearea>c1.surfaceearea 其中c2.1区域为空; 选择c1* 来自c1国 不存在的地方 挑选* 来自c2国 其中c2.region=c1.region 和c2.surfaceearea>c1.surfaceearea ;
您使用的是MySQL还是SQL Server?他们不是一回事。不,这不对。但是你的问题可以用两种不同的方式来解释。您是否在寻找表面积大于该地区所有其他国家总和的国家?或者您正在寻找该地区表面积最大的国家?如果您想列出这些国家,您需要在SELECT语句中包含“名称”。@Siyual am使用MySQL,我正在寻找该地区表面积最大的国家,或大于区域的平均值(包括选择中的名称)将不起作用。max函数不会返回行中的所有值,结果是不确定的。一个子选择将是必需的。我实际上发布了完全相同的东西,但为什么'A.surfaceearea>t.maxarea'?“>”不应该是“=”?@ghehy你说得对。。不等于>。。。取消删除你的帖子。。这是一个很好的答案。没必要,我们只相隔几秒钟,但不想用基本上重复的帖子把空间搞得一团糟。我以为你刚打错了。@ghenghy一秒钟都不用担心。。取消删除你的答案是一个很好的答案,就像我的一样。S.O.不仅仅是一场比赛。谢谢,但我已经得到了一个答案,因为在过去的这个场景中,我是一个重复的答案。我会把这出戏传下去。亨希、斯凯西和斯宾塞7593两个很好的答案。一切都好!!