假设我们有一张eidInfo表,其中字段time是datatime类型
如果我们想查询feid是16998291并且ftime时间距离我们最近的数据怎么查询呢?
sql如下:
SELECt a.feid, a.ftime FROM eidInfo AS a JOIN ( SELECt feid, MAX(ftime) AS max_date FROM eidInfo WHERe feid='16998291') AS b WHERe a.feid = b.feid AND a.ftime = b.max_date
查询结果如下:
参考:
The Rows Holding the Group-wise Maximum of a Certain Column
How to SELECT by MAX(date)