不明确的列名称错误

时间:2014-12-02 13:58:18

标签: mysql syntax-error

我知道有很多人发布了这个,但他们似乎没有像我一样的错误。

当我运行此声明时,我得到了ERROR 1052。

mysql> SELECT module_staff.StaffName AS Staff, 
              SUM(modules.HoursPerWeek) AS Total_Worked 
       FROM module_staff, modules 
       WHERE ModuleCode IN (SELECT ModuleCode
                            FROM module_staff 
                            WHERE StaffName ='Nigel Cosworth');

但是下面这段代码确实有效。

mysql> SELECT Title, 
              SUM(modules.HoursPerWeek) AS Total_Worked 
       FROM modules 
       WHERE ModuleCode IN(SELECT ModuleCode 
                           FROM module_staff
                           WHERE StaffName ='Nigel Cosworth');

module_staff具有ModuleCode和StaffName

和仅需要模块的列是HoursPerWeek,Title和ModuleCode

2 个答案:

答案 0 :(得分:0)

我认为错误在于JOIN。 尝试更新您的查询,如下所示:

SELECT ms.StaffName AS Staff, SUM(m.HoursPerWeek) AS Total_Worked 
FROM module_staff ms
LEFT JOIN modules m
ON ms.ModuleCode = m.ModuleCode
WHERE ms.StaffName ='Nigel Cosworth';

答案 1 :(得分:0)

   SELECT ms.StaffName AS Staff, SUM(m.HoursPerWeek) AS Total_Worked 
FROM module_staff ms
LEFT JOIN modules m
ON ms.ModuleCode = m.ModuleCode
WHERE ms.StaffName ='Nigel Cosworth';

适合你......