模糊的Mysql列

时间:2017-03-31 22:05:55

标签: mysql ambiguous

我有这个查询

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 (*),所以我不明白为什么?

3 个答案:

答案 0 :(得分:1)

您选择的表格的产品包括两个相同名称的列,如下所示:

ON t.employee_id=et.employee_id

timesheetsemployeetimesheets都有该列。由于JOIN子句将确保产品中的这两列始终具有相同的值,因此您指定的列可能并不重要。任何一个人都会做这个工作:

WHERE t.employee_id='6748372'

虽然你可能想在两者上运行DESCRIBE,但看看性能是否有任何差异。

答案 1 :(得分: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'

答案 2 :(得分: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别名