Error code: 1052 Column ‘xxx’ in field list is ambiguous causes and solutions
1. Example
Query employee ID employee_id
and its corresponding department name department_name
.
SELECT employee_id, department_name, department_id FROM employees, departments WHERE employees.`department_id` = departments.`department_id`;
The query results in the following error:
Error code: 1052 Column 'department_id' in field list is ambiguous
2. Reason for error
The “Department ID” department_id
in line 1 does not indicate which of the 2 tables it is. Because the field “Department ID” department_id
with the same name exists both in the employees table employees
and in the departments table departments
. So you need to indicate which table the “department number” department_id
in line 1 is from.
3. Correct spelling
SELECT employee_id, department_name, employees.department_id FROM employees, departments WHERE employees.`department_id` = departments.`department_id`;
Change department_id
in the first line of code to employees.department_id
, indicating that the field “department number” department_id
is from the employee table employees
. You can query correctly, I hope this article is helpful to you.
search result: