2017-03-31 153 views
0

我有這個疑問曖昧的Mysql列

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' 

它給了我這個錯誤

1052 - 列在where子句是曖昧

我 'EMPLOYEE_ID'看了herehere但我沒有使用all (*)所以我不明白爲什麼?

+1

只是做' t.employee_id ='6748372'' – bernie

+0

你有't.employee_id'和'et.employee_id',對你來說它們是相同的並且保持相同的信息,但是MySQL不會跳到結論,你必須指定哪一個。只要引用'employee_id'就會變得模糊不清。 – Havenard

回答

1

您需要where節定義表的別名,像這樣

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' 

如果從表employeetimesheets列設置et別名

1

要從中選擇表的產品包括兩個同名的列,這裏明顯:

ON t.employee_id=et.employee_id 

兩個timesheetsemployeetimesheets有列。由於JOIN子句將確保產品中的這兩列始終具有相同的值,因此您指定的可能無關緊要。任何一個會做的工作:

WHERE t.employee_id='6748372' 

雖然你可能要運行在既有DESCRIBE,看看是否有在性能上有什麼區別。

1

您必須指定您指的是哪張表。試試這個,並注意更新WHERE行:

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 timeinyear='2017' 
AND timeinmonth='March' 
AND isWeekNumber='1'