在 MySQL 中,最简单的方法可能是使用substring_index()
/group_concat()
技巧:
select substring_index(group_concat(date order by temp asc), ',', 1) as minTempDate,
substring_index(group_concat(date order by temp desc), ',', 1) as maxTempDate,
substring_index(group_concat(date order by dewpoint asc), ',', 1) as minDPDate,
substring_index(group_concat(date order by dewpoint desc), ',', 1) as maxDPDate,
substring_index(group_concat(date order by humidity asc), ',', 1) as minHumidityDate,
substring_index(group_concat(date order by humidity desc), ',', 1) as maxHumidityDate
from table t;
另一种方法是union all
像这样使用:
(select date, 'temp-min', temp from table t order by temp asc limit 1)
union all
(select date, 'temp-max', temp from table t order by temp desc limit 1)
union all
(select date, 'humidity-min', humidity from table t order by humidity asc limit 1)
union all
(select date, 'humidity-max', humidity from table t order by humidity desc limit 1)