SQL Select 来自 table 的最大值和最小值
SQL Select MAX and MIN value from a table
我正在使用 MySql。我有一个 table 是我从 table 个国家/地区创建的。 table 拉动大陆并计算 table 中的国家数。
Table 创建工作正常。然后我想拉动国家数量最多的大陆和国家数量最少的大陆。
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));
我得到了整个 table:
continent number_of_countries
Antarctica 5
South America 14
Oceania 28
North America 37
Europe 46
Asia 51
Africa 58
不过我想要的是:
continent number_of_countries
Antarctica 5
Africa 58
抱歉,我不知道如何在此处制作 table,所以行很乱。
还有,有什么办法可以:
- 将查询合并为一个并获得所需的结果?
- 按国家数量对各大洲进行排名(有一个新的排名列,例如非洲将是 1,亚洲将是 2,等等)?
实现此目的的一种方法是使用 UNION,它可以让您结合
来自多个查询的结果(前提是它们具有相同的列)。例如,
-- 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
)
由于您已经有一个名为 cops
的 table,它包含每个大陆的国家数量,您可以这样做:
-- 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);
或类似这样的内容:
select *
from cops
where number_of_countries in (
(select min(number_of_countries) from cops),
(select max(number_of_countries) from cops)
);
关于你的第二个问题:使用用户变量:
select cops.*, @n := n + 1 as rank
from (select @n := 0) as init,
cops
order by number_of_countries desc
您查询中的 WHERE
子句有误。用这样的东西替换它应该会给你你正在寻找的结果:
where number_of_countries = ( SELECT MIN(number_of_countries) FROM cops )
or number_of_countries = ( SELECT MAX(number_of_countries) FROM cops )
还有其他查询模式可以提供相同的结果。作为一种模式的示例,使用连接到内联视图:
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)
我正在使用 MySql。我有一个 table 是我从 table 个国家/地区创建的。 table 拉动大陆并计算 table 中的国家数。
Table 创建工作正常。然后我想拉动国家数量最多的大陆和国家数量最少的大陆。
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));
我得到了整个 table:
continent number_of_countries
Antarctica 5
South America 14
Oceania 28
North America 37
Europe 46
Asia 51
Africa 58
不过我想要的是:
continent number_of_countries
Antarctica 5
Africa 58
抱歉,我不知道如何在此处制作 table,所以行很乱。
还有,有什么办法可以:
- 将查询合并为一个并获得所需的结果?
- 按国家数量对各大洲进行排名(有一个新的排名列,例如非洲将是 1,亚洲将是 2,等等)?
实现此目的的一种方法是使用 UNION,它可以让您结合 来自多个查询的结果(前提是它们具有相同的列)。例如,
-- 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
)
由于您已经有一个名为 cops
的 table,它包含每个大陆的国家数量,您可以这样做:
-- 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);
或类似这样的内容:
select *
from cops
where number_of_countries in (
(select min(number_of_countries) from cops),
(select max(number_of_countries) from cops)
);
关于你的第二个问题:使用用户变量:
select cops.*, @n := n + 1 as rank
from (select @n := 0) as init,
cops
order by number_of_countries desc
您查询中的 WHERE
子句有误。用这样的东西替换它应该会给你你正在寻找的结果:
where number_of_countries = ( SELECT MIN(number_of_countries) FROM cops )
or number_of_countries = ( SELECT MAX(number_of_countries) FROM cops )
还有其他查询模式可以提供相同的结果。作为一种模式的示例,使用连接到内联视图:
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)