mysql 先取最小值再取最大值
mysql get max value after min value
在mysqltable中,我想得到最小值之后的最大值。
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 不是目标行。
因为这个 max(1.60) 不是在 min value(1.10)
之后
id 3,值 1.40 是目标行。
因为这个最大值(1.40)在最小值(1.10)之后
应忽略最小值之前的所有值。
我想得到这一行(id 3, value 1.40)
我需要哪个 sql 查询?
我会使用子查询和限制。 table 名称和列名只是一个示例,因此请将其调整为您正确的 table 名称和列名。祝你好运
select * from table
inner join (
select id as minvalid, min(value) as minvalue from table
) subQuery on value > minvalue
limit 1
你可以运行像这样:
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 |
+------+---------------------+------+
想法是首先使用 select min(vl) from t
.
从 table 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)
)
然后,我们得到具有该最大值的行。
在mysqltable中,我想得到最小值之后的最大值。
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 不是目标行。 因为这个 max(1.60) 不是在 min value(1.10)
之后id 3,值 1.40 是目标行。 因为这个最大值(1.40)在最小值(1.10)之后 应忽略最小值之前的所有值。 我想得到这一行(id 3, value 1.40)
我需要哪个 sql 查询?
我会使用子查询和限制。 table 名称和列名只是一个示例,因此请将其调整为您正确的 table 名称和列名。祝你好运
select * from table
inner join (
select id as minvalid, min(value) as minvalue from table
) subQuery on value > minvalue
limit 1
你可以运行像这样:
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 |
+------+---------------------+------+
想法是首先使用 select min(vl) from t
.
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)
)
然后,我们得到具有该最大值的行。