我有这个结构...
ID date Value
732 2019-07-03 15:04:42 283.0
732 2019-07-03 15:24:07 283.2
732 2019-07-03 15:43:44 283.4
732 2019-07-03 16:03:37 283.5
732 2019-07-03 16:22:47 283.7
732 2019-07-03 16:42:25 283.8
733 2019-06-14 13:24:01 226.3
733 2019-06-14 15:09:38 228.1
733 2019-06-17 08:39:59 291.5
733 2019-06-17 10:46:20 291.7
我可以做一个简单的选择,例如:
select *
from table
where id=732 and date < '2019-07-03 16:03:37'
and date > '2019-07-03 15:24:07'
但是它给了我指定时间范围内的所有数据,我只需要获取选择时间范围内最早的DATE的行和最新的DATE的行,所以,只有两行。
我尝试使用 MIN 和 MAX 但没有成功。
版本:修复英语语言错误
提前致谢
最佳答案
一个简单的方法使用union all
和limit
:
(select t.*
from t
order by date asc
limit 1
) union all
(select t.*
from t
order by date desc
limit 1
)
关于mysql - 使用 SQL 获取表中最早和最新插入的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56877596/