简体   繁体   English

SQL从表中选择MAX和MIN值

[英]SQL Select MAX and MIN value from a table

I'm using MySql. 我正在使用MySql。 I have a table that I created from a table of countries. 我有一个从国家表格创建的表格。 The table pulls the continent and counts the number of countries in that table. 表格将拉动大陆,并计算该表格中的国家/地区数量。

Table creation works fine. 表创建工作正常。 I want to then pull the continent with the highest number of countries and the continent with the lowest number of countries. 然后,我想拉动国家数量最多的大陆和国家数量最少的大陆。

create table cops as (select 
continent,
count(name) as number_of_countries
from country
group by continent);

select 
continent,
number_of_countries
from cops
where number_of_countries = (select MIN(number_of_countries)) OR (select MAX(number_of_countries));

I'm getting the entire table: 我得到整个桌子:

continent   number_of_countries
Antarctica  5
South America   14
Oceania 28
North America   37
Europe  46
Asia    51
Africa  58

All I want though is: 我只想要:

continent   number_of_countries
Antarctica  5
Africa  58

Sorry I don't know how to make a table on here so the rows are screwy. 抱歉,我不知道如何在此处制作桌子,所以行很麻烦。

Also, is there any way to: 另外,有什么方法可以:

  1. Combine the queries into one and get the desired result? 将查询合并为一个并获得所需的结果?
  2. Rank the continents by the number of countries (having a new rank column so for example africa would be 1, asia 2, etc.)? 通过国家/地区对大陆进行排名(具有新的排名列,例如,非洲将为1,亚洲为2,等等)?

One way to accomplish this is using UNION which can allow you to combine results from multiple queries (provided they have identical columns). 一种实现此目的的方法是使用UNION ,它可以让您合并来自多个查询的结果(前提是它们具有相同的列)。 Eg, 例如,

-- Get continent with greatest number of countries.
SELECT
    continent,
    number_of_countries
FROM cops
WHERE continent = (
    SELECT continent
    FROM cops
    ORDER BY number_of_countries DESC
    LIMIT 1
)

UNION

-- Get continent with least number of countries.
SELECT
    continent,
    number_of_countries
FROM cops
WHERE continent = (
    SELECT continent
    FROM cops
    ORDER BY number_of_countries ASC
    LIMIT 1
)

Since you already have a table called cops which holds the number of countries per continent, you could do something like this: 由于您已经有了一个名为cops的表格,该表格可以保存每个大洲的国家数量,因此您可以执行以下操作:

-- The UNION approach
select *
from cops
where number_of_countries = (select min(number_of_countries) from cops)
union
select *
from cops
where number_of_countries = (select max(number_of_countries) from cops);

or something like this: 或类似这样的东西:

select *
from cops
where number_of_countries in (
          (select min(number_of_countries) from cops),
          (select max(number_of_countries) from cops)
      );

And for your second question: use user variables: 关于第二个问题:使用用户变量:

select cops.*, @n := n + 1 as rank
from (select @n := 0) as init,
     cops
order by number_of_countries desc

The WHERE clause in your query is wrong. 您的查询中的WHERE子句是错误的。 Replacing that with something like this should give you the result you are looking for: 用类似这样的东西代替应该给您想要的结果:

  where number_of_countries = ( SELECT MIN(number_of_countries) FROM cops )
     or number_of_countries = ( SELECT MAX(number_of_countries) FROM cops )

There are other query patterns that will give an equivalent result. 还有其他查询模式将给出相同的结果。 As an example of one pattern, using a join to an inline view: 作为一种模式的示例,使用对内联视图的联接:

  SELECT c.continent
       , c.number_of_countries
    FROM ( SELECT MIN(n.number_of_countries) AS min_noc
                , MAX(n.number_of_countries) AS max_noc
             FROM cops n
         ) m
    JOIN cops c
      ON c.number_of_countries IN (m.min_noc,m.max_noc)

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

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