2
SELECT TIME(ADDTIME(departure, SEC_TO_TIME(SUM(jptl2.run_time)))) AS departure,
service.line_name AS line,
bus.registration
FROM vehicle_journey
LEFT JOIN bus ON bus.journey = vehicle_journey.code AND
ADDTIME(TIME(NOW()), SEC_TO_TIME(3600)) > vehicle_journey.departure AND
ADDTIME(TIME(NOW()), SEC_TO_TIME(3600)) < ADDTIME(vehicle_journey.departure, SEC_TO_TIME(SUM(jptl.run_time)))
INNER JOIN service ON service.reference = vehicle_journey.service_reference
INNER JOIN vehicle_journey_days ON vehicle_journey_days.journey_reference = vehicle_journey.reference
INNER JOIN journey_pattern ON journey_pattern.reference = vehicle_journey.journey_pattern_reference
INNER JOIN journey_pattern_timing_link jptl ON jptl.section = journey_pattern.journey_pattern_section
INNER JOIN journey_pattern_timing_link jptl2 ON jptl2.section = journey_pattern.journey_pattern_section AND jptl2.ID < jptl.id
WHERE jptl.stop = '1900HA030334' AND vehicle_journey_days.day = 'MondayToFriday'
GROUP BY vehicle_journey.id
上面的SQL查詢返回的:#1111 - Invalid use of group function
SQL無效使用組功能
我知道,這個問題是由第三ON
的LEFT JOIN bus
子句中造成的。我該如何重寫查詢,以便根據相同的要求保存相同的目的和連接,但沒有組功能錯誤?
我可以直接將SEC_TO_TIME(SUM(jptl.run_time))
作爲子查詢嗎?
EG:(SELECT SEC_TO_TIME(SUM(jptl.run_time)))