首页 > 文章列表 > MySQL错误代码:1052 Column 'xxx' in field list is ambiguous的原因和解决

MySQL错误代码:1052 Column 'xxx' in field list is ambiguous的原因和解决

mysql
497 2023-05-15

错误代码: 1052 Column ‘xxx’ in field list is ambiguous出现的原因和解决方法

一、 例子

查询员工编号 employee_id 和其对应的部门名称 department_name

SELECT employee_id, department_name, department_id

FROM employees, departments

WHERE employees.`department_id` = departments.`department_id`;

查询结果出现以下错误:

错误代码: 1052 Column 'department_id' in field list is ambiguous

二、 错误原因

第 1 行代码中“部门编号” department_id 没有指明是 2 个表中的哪一个表。因为在员工表 employees 中和部门表 departments 中都存在同名的字段“部门编号” department_id 。因此需要指明第 1 行代码中“部门编号” department_id 是来自哪个表。

三、 正确写法

SELECT employee_id, department_name, employees.department_id

FROM employees, departments

WHERE employees.`department_id` = departments.`department_id`;

把第 1 行代码中的 department_id 改成 employees.department_id ,说明字段“部门编号” department_id 是来自员工表 employees 中的。即可正确查询,希望本文对你有帮助。

查询结果:

总结