2011-09-07 138 views
2

我有以下SQL語句:錯誤代碼:1054未知列

SELECT efforts.user_id, project_tasks.task_name, sum(hours) 
FROM efforts, users, project_tasks 
INNER JOIN PROJECT_TASKS pu ON efforts.project_task_id = pu.id 
INNER JOIN USERS u ON efforts.users_id = u.id 
WHERE project_tasks.project_id = '2'; 

當我運行它,我得到以下錯誤:

Error Code: 1054. Unknown column 'efforts.project_task_id' in 'on clause' 

爲什麼會出現這個錯誤?

Project_task_id屬於工作表

Image of my efforts table

更新時間:

SELECT u.full_name, pu.task_name, hours 
FROM efforts 
INNER JOIN project_tasks pu ON efforts.project_task_id = pu.id 
INNER JOIN users u ON efforts.user_id = u.id 
GROUP BY user_id, task_name 
+0

沒有u必須努力表此列? –

+0

請提供努力表的表格描述.. – StevieG

+0

我已添加下面的圖像以顯示我的表格 – David

回答

7

你的語法是錯誤的,它應該是:

SELECT efforts.user_id, pu.task_name, sum(hours) 
FROM efforts 
INNER JOIN PROJECT_TASKS pu ON efforts.project_task_id = pu.id 
INNER JOIN USERS u ON efforts.user_id = u.id 
WHERE pu.project_id = '2'; 
+0

最後一行應該是:WHERE pu.project_id ='2'; –

+0

@KlasLindbäck我試過這個,它沒有工作我得到了這個錯誤'錯誤代碼:1054.'字段列表'中的未知列'project_tasks.task_name' – David

+0

現在再試一次 – StevieG

1

需要注意的是,據我所知,MySQL是區分大小寫的表名(不在列名上)......這可能會在查詢中造成一些麻煩......?隨着StevieG的校正

+0

你的建議實際上工作和解決了我在掙扎的錯誤。 –

相關問題