尝试加入这3个表但获得#1052 - 字段列表中的“TaskID”列不明确。请帮忙
select P.ProjectColor,TimeCardID,UserName,TaskName,TaskID,MAX (Punch), TotalHours,Running,LASTUPDATE
from T_TIMECARD as E, T_TASK as A, T_PROJECT AS P
where E.TaskID = A.TaskID
AND P.ProjectID = A.ProjectID
group by Punch
order by Punch desc;
答案 0 :(得分:1)
学习使用显式join
语法。导致该问题是因为两个表中的列相同。是的,他们=
表示他们有相同的价值观,但你必须明确:
Select P.ProjectColor, TimeCardID, UserName, TaskName, A.TaskID,
MAX(Punch), TotalHours, Running, LASTUPDATE
from T_TIMECARD E JOIN
T_TASK A
ON E.TaskID = A.TaskID JOIN
T_PROJECT P
ON P.ProjectID = A.ProjectID
group by P.ProjectColor, TimeCardID, UserName, TaskName, A.TaskID, TotalHours, Running, LASTUPDATE
order by MAX(Punch) desc;
您的group by
还应提及不在聚合列中的所有列。
顺便说一句,我怀疑这个查询能做你真正想要的。如果是这种情况,您应该询问另一个带有样本数据和所需结果的问题。不要编辑此问题,因为编辑将使答案无效。
答案 1 :(得分:0)
select子句引用了TaskID
,但它并不知道你的意思是E.TaskID
还是A.TaskID
。您需要具体并为select子句中出现在多个表中的所有列设置别名。
group by
也会遇到问题:如果按punch
进行分组,那么除了punch
之外,select子句中的所有其他列都必须是聚合函数。
答案 2 :(得分:0)
使用别名....
select P.ProjectColor,TimeCardID,UserName,TaskName, **A.TaskID** ,MAX (Punch),
TotalHours,Running,LASTUPDATE
from T_TIMECARD as E, T_TASK as A, T_PROJECT AS P
where E.TaskID = A.TaskID
AND P.ProjectID = A.ProjectID
group by Punch
order by Punch desc;
答案 3 :(得分:0)
谢谢你的帮助。你们很棒。它的工作原理
DROP VIEW IF EXISTS RECENT_TIMECARD;
创建视图RECENT_TIMECARD AS
SELECT P.ProjectColor,TimeCardID,UserName,TaskName,A.TaskID,MAX(Punch)AS LASTUDATE,TotalHours,Running 来自T_TIMECARD E. JOIN T_TASK A ON E.TaskID = A.TaskID 加入T_PROJECT P ON P.ProjectID = A.ProjectID GROUP BY P.ProjectColor,TimeCardID,UserName,TaskName,A.TaskID,TotalHours,Running ORDER BY MAX(Punch)DESC