MYSQL在where子句中的平均时间

时间:2015-10-24 20:21:54

标签: mysql performance

表格中的两列 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>

2 个答案:

答案 0 :(得分:0)

非常简单: 具有相应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;

只是值&lt; 10s

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

答案 1 :(得分:0)

这可以避免两次调用TIMEDIFF。 (它可能不会加速注意事项。)

Select  rowId,
        TIME_TO_SEC(TIMEDIFF(cityWalkEndTime, cityWalkStartTime)) AS diff
    from  walks
    HAVING  diff < 10