max() 函数

发布于 2024-10-20 10:48:31 字数 266 浏览 0 评论 0原文

我创建了下表。

create table man(id integer, name varchar(20), city varchar(20), age integer)

我想找到年龄最大的人的名字。

因此,我运行了以下查询,

select name, max(age) from man group by name;

它按年龄升序显示了所有人的姓名。出了什么问题?

I create the following table.

create table man(id integer, name varchar(20), city varchar(20), age integer)

I want to find the name of the person with maximum age.

So, I ran following query,

select name, max(age) from man group by name;

It shows name of all person in ascending order with their age. What is going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

牵你手 2024-10-27 10:48:31

如果你想获得年龄较大的人,你可以这样做:

select name, age from man order by age desc limit 1;

对于使用 SQL Server 的查询:

select top 1 name, age from man order by age desc;

你的查询正在做其他事情。 max 使用您的 group 子句来进行计算。由于您是按姓名分组,因此 max(age) 是同名人员的最大年龄。

If you want to obtain the person with the greater age, you can do :

select name, age from man order by age desc limit 1;

For a query working with SQL Server :

select top 1 name, age from man order by age desc;

Your query is doing something else. max uses your group clause to do its computation. Since you're grouping by name, max(age) is the maximal age for people with the same name.

源来凯始玺欢你 2024-10-27 10:48:31

我很难理解你在这里做什么。在你的模型中一个人可以有多个年龄吗?

反正。如果你想要表 man 中的最大年龄,你需要运行:

select max(age) from man

Now who is this?如果您将 name 添加到查询中,数据库引擎将返回所有名称(因为您要求提供所有名称,而不是特定名称;分组不会有帮助,因为大多数名称已经不同)与最大。 age 列的值。

您需要的是一个子查询:

select m1.name
from man m1
where m1.age = (
    select max(m2.age)
    from man m2
)

它选择年龄与集合中最高年龄相同的所有元素。

I struggle to understand what you're doing here. A person can have more than a single age in your model?

Anyway. If you want the maximum age in the table man, you need to run:

select max(age) from man

Now who is this? If you add name to the query, the DB engine will return all names (since you asked for all of them, not a specific one; grouping won't help since most names will already be distince) combined with the max. value of the age column.

What you need is a subquery:

select m1.name
from man m1
where m1.age = (
    select max(m2.age)
    from man m2
)

This selects all elements where the age is the same as the highest age in the set.

滥情哥ㄟ 2024-10-27 10:48:31

对于每个名字,它都会为您提供所有使用该名字的人的最大年龄。 (这就是你所要求的,不是吗?)

For each name it's giving you the maximum age of all the people with that name. (That's what you asked for, isn't it?)

反差帅 2024-10-27 10:48:31

选择名称。 max(age) 来自男子组的姓名;

应该是

SELECT name, max(age) FROM man GROUP BY name;

您也可以尝试

SELECT name,age FROM man ORDER BYage DESC

也许更多关于您期望的结果的信息会有帮助?

select name. max(age) from man gruop by name;

Should be

SELECT name, max(age) FROM man GROUP BY name;

You could also try

SELECT name, age FROM man ORDER BY age DESC

Maybe a little more information about the results you were expecting would be helpfull?

伪装你 2024-10-27 10:48:31
  SELECT NAME, AGE
    FROM MAN
GROUP BY NAME, AGE
  HAVING AGE = (SELECT MAX (AGE) FROM MAN)
  SELECT NAME, AGE
    FROM MAN
GROUP BY NAME, AGE
  HAVING AGE = (SELECT MAX (AGE) FROM MAN)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文