Search code examples
mysqlambiguous

Ambiguous Mysql Column


I have this query

SELECT t.employee_id, t.timeinhour,t.timeouthour, 
SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(t.timeouthour, t.timeinhour)))) 
AS Duration FROM timesheets t 
INNER JOIN employeetimesheets et 
ON t.employee_id=et.employee_id
WHERE employee_id='6748372'
AND timeinyear='2017' 
AND timeinmonth='March' 
AND isWeekNumber='1'

It gives me this error

1052 - Column 'employee_id' in where clause is ambiguous

I have looked here and here but I'm not using all (*) so I don't understand why?


Solution

  • You need define table alias in where section, like this

    SELECT t.employee_id, t.timeinhour,t.timeouthour,
    SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(t.timeouthour, t.timeinhour))))
    AS Duration FROM timesheets t
    INNER JOIN employeetimesheets et
    ON t.employee_id=et.employee_id
    WHERE t.employee_id='6748372'
    AND t.timeinyear='2017'
    AND t.timeinmonth='March'
    AND t.isWeekNumber='1'
    

    If column from table employeetimesheets set et alias