gpt4 book ai didi

mysql 在最小值后获取最大值

转载 作者:行者123 更新时间:2023-12-04 08:01:04 27 4
gpt4 key购买 nike

在 mysql 表中,我想获得最小值之后的最大值。

SELECT * from `table`
result:

id date value
-- ------------------- -----
1 2021-03-03 13:14:05 1.15
2 2021-03-03 13:14:06 1.32
3 2021-03-03 13:14:07 1.40
4 2021-03-03 13:14:08 1.38
5 2021-03-03 13:14:01 1.55
6 2021-03-03 13:14:02 1.60
7 2021-03-03 13:14:03 1.30
8 2021-03-03 13:14:04 1.10
但我必须按日期排序,
SELECT * from `table` ORDER by date
result:

id date value
-- ------------------- -----
5 2021-03-03 13:14:01 1.55
6 2021-03-03 13:14:02 1.60 # this is not target row.
7 2021-03-03 13:14:03 1.30
8 2021-03-03 13:14:04 1.10 # min value
1 2021-03-03 13:14:05 1.15
2 2021-03-03 13:14:06 1.32
3 2021-03-03 13:14:07 1.40 # this is TARGET row.(max after min)
4 2021-03-03 13:14:08 1.38
按日期排序后,
我想获得最小值(1.10)之后的最大值(1.40)
id 6,值 1.60 不是目标行。
因为这个最大值(1.60)不是在最小值(1.10)之后
id 3,值 1.40 是目标行。
因为这个最大值(1.40)在最小值(1.10)之后
应忽略最小值之前的所有值。
我想得到这一行(id 3,值 1.40)
我需要哪个 sql 查询?

最佳答案

你可以运行这样的东西:

select * from t
where vl = (
select max(vl) from t
where dt > (
select dt from t where vl = (select min(vl) from t)
)
);

+------+---------------------+------+
| id | dt | vl |
+------+---------------------+------+
| 3 | 2021-03-03 13:14:07 | 1.40 |
+------+---------------------+------+
这个想法是从表 t 中获取最小值第一次使用 select min(vl) from t .
然后,我们使用 select dt from t where vl = (select min(vl) from t) 获得该值的日期。 .
然后,我们使用以下方法获得该日期之后显示的最大值:
select max(vl) from t
where dt > (
select dt from t where vl = (select min(vl) from t)
)
然后,我们得到具有该最大值的行。

关于mysql 在最小值后获取最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66466448/

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