Error Code: 1054. Unknown column

2019-07-02 12:30发布

问题:

I have the following SQL statement:

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'; 

And when I run it I get the following error:

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

Why am I getting this error?

Project_task_id belongs to efforts table

Image of my efforts table

Updated:

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

回答1:

Your syntax is wrong, it should be:

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';  


回答2:

Note that as far as I know, MySQL is case-sensitive on table names (not on column names)... That might cause some trouble in your query...? Along with StevieG's correction