gpt4 book ai didi

mysql - 记录组中 N 列的最大值

转载 作者:行者123 更新时间:2023-11-29 09:37:36 25 4
gpt4 key购买 nike

这是典型的“每组最大值”的扩展,如下所示:https://stackoverflow.com/a/12102216/510144

我需要的是更进一步 - 以级联方式获取多个列中具有最大值的行。

给定:

id | effective_time | revision | score
1 | 0 | 0 | 5
1 | 1 | 0 | 10
1 | 1 | 1 | 15
1 | 2 | 0 | 20

我需要:

1 |               1 |        1 | 15

用简单的英语来说:id 1 的项目的值是多少,其中最大有效时间 小于 2(最大修订) effectime_time

最佳答案

假设没有平局(只有一行具有最大值),您可以执行以下操作:

select *
from napoleon
where (effective_time, revision) < (2, 0)
order by effective_time desc, revision desc
limit 1

结果:

id  effective_time  revision  score
-- -------------- -------- -----
1 1 1 15

作为引用,我用来测试的数据脚本是(你没有提到表名,所以我使用了“napoleon”):

create table napoleon (
id int,
effective_time int,
revision int,
score int
);

insert into napoleon (id, effective_time, revision, score) values
(1, 0, 0, 5),
(1, 1, 0, 10),
(1, 1, 1, 15),
(1, 2, 0, 20);

关于mysql - 记录组中 N 列的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57260134/

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