从表中获得正确的结果

时间:2015-05-10 19:49:59

标签: mysql sql

我有两个表stopsarrivaletimes如果找到该位置而不是像现在这样的路径,我怎样才能获得与stop_id相关的route。例如,如果找到停止ABC,我想获得此stop_id的路由取决于当前时间。

enter image description here

enter image description here

结果查询:

SELECT route from arrivaltimes INNER JOIN stops ON  
    arrivaltimes.stop_id=stops.stop_id WHERE 
    time_format(arrivaltime,'%H:%i')= time_format(curtime() ,'%H:%i')

enter image description here

但我希望得到路由1作为结果,而不是所有与stop_id相关的路由

1 个答案:

答案 0 :(得分:0)

PFB更正了位置信息:

SELECT 
    route FROM arrivaltimes 
INNER JOIN 
    stops ON arrivaltimes.stop_id = stops.stop_id 
INNER JOIN 
    stops ON arrivaltimes.location = stops.location 
WHERE 
    time_format(arrivaltime,'%H:%i') = time_format(curtime() ,'%H:%i')
ORDER BY 
    arrivaltimes.stop_id DESC LIMIT 1;

让我们知道结果。

相关问题