表cityWalkStartTime和cityWalkEndTime中的两列,带有时间戳。

寻找一个查询,该查询将返回行ID,以最小的时间差。

Select rowId,TIME_TO_SEC(TIMEDIFF(cityWalkEndTime,cityWalkStartTime)) from walks where  <time diffence is minimum in the entire database>


另外,如果我想要时差小于10秒的行。

Select rowId,TIME_TO_SEC(TIMEDIFF(cityWalkEndTime,cityWalkStartTime)) from walks where  <time diffence is < 10 seconds>

最佳答案

很简单:
具有相应rowId的数据库中的最小值:

Select rowId,MIN(TIME_TO_SEC(TIMEDIFF(cityWalkEndTime,cityWalkStartTime)))
from walks
GROUP BY rowId
ORDER BY MIN(TIME_TO_SEC(TIMEDIFF(cityWalkEndTime,cityWalkStartTime))) ASC
LIMIT 1;


仅值
Select rowId,TIME_TO_SEC(TIMEDIFF(cityWalkEndTime,cityWalkStartTime))
from walks
where TIME_TO_SEC(TIMEDIFF(cityWalkEndTime,cityWalkStartTime))<10;

10-07 23:07