Search code examples
mysqlidentifier

The multi-part identifier "t1.LogTime" could not be bound. What I missed?


I need some data from log, for example who drive the car XY day.

The Query is:

SELECT
t1.LogTime, 
t1.UnitId, 
t1.Alarm, 
t1.Speed, 
t1.Km, 
t1.GPSVisibleSats, 
t1.InputMask, 
t1.AX0, 
t1.Country, 
t1.City, 
t1.Street,
t3.Name
FROM dbo.t_log AS t1
LEFT JOIN
  (SELECT TOP 1 * FROM dbo.t_driver_log AS v1 WHERE v1.UnitId = '391.03.016' AND t1.LogTime BETWEEN v1.StartTime AND v1.StopTime) AS t2 ON ( t1.UnitId = t2.UnitId )
LEFT JOIN dbo.t_driver AS t3 ON ( t2.DriverId = t3.DriverId ) 
WHERE t1.UnitId = '391.03.016' AND t1.LogTime BETWEEN '2016-10-04 00:00:00' AND '2016-10-04 23:59:59'

what's wrong with this guys? The error is came from the subquery.

SQL Schema


Solution

  • Problem with subquery Since you cant directly access t1.LogTime in sub-query directly, try updated script, try to move AND t1.LogTime BETWEEN t2.StartTime AND t2.StopTime this condition on JOIN

    ...
    FROM dbo.t_log AS t1
    LEFT JOIN
    (SELECT TOP 1 * 
     FROM dbo.t_driver_log AS v1 
     WHERE v1.UnitId = '391.03.016'
    ) AS t2 ON ( t1.UnitId = t2.UnitId  AND t1.LogTime BETWEEN t2.StartTime AND t2.StopTime)
    LEFT JOIN dbo.t_driver AS t3 ON ( t2.DriverId = t3.DriverId ) 
    WHERE t1.UnitId = '391.03.016' AND t1.LogTime BETWEEN '2016-10-04 00:00:00' AND '2016-10-04 23:59:59'