gpt4 book ai didi

mysql - sql where条件获取第一个和第二个最大值

转载 作者:行者123 更新时间:2023-11-29 01:58:53 27 4
gpt4 key购买 nike

我正在处理 SQL,但遇到以下问题:

    select * from(
select tname,teacher.tid,grade from teacher
inner join
_view
on(_view.tid=teacher.tid))as D
group by grade
where // what should I do here to get the rows having the first and the second maxium values?
order by grade desc,tid;

我只想选择具有第一个最大值和第二个最大值的行,从昨天开始我尝试了很多东西,但没有任何好处!!

当我使用诸如 MAX、COUNT 或 AND 之类的东西时,我得到了聚合函数的错误,请帮助我解决这个问题,因为我已尽我所能!!

最佳答案

我相信你能做到:

select tname,teacher.tid,grade 
from teacher
inner join _view on _view.tid=teacher.tid
order by grade desc,tid
limit 2;

LIMIT 2 为您提供刚刚从 SELECT 中获得的列表的前两行。由于您按等级 desc 排序,因此将返回具有两个最高等级的记录。

来自docs :

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

您还进行了派生查询,但我不明白如果您不对它做任何事情,您为什么需要它。 GROUP BY 应该不是必需的。

关于mysql - sql where条件获取第一个和第二个最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20475181/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com