MySQL错误代码:1052 Column 'xxx' in field list is ambiguous的原因和解决
错误代码: 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
中的。即可正确查询,希望本文对你有帮助。
查询结果:
总结
到此这篇关于MySQL错误代码:1052 Column 'xxx' in field list is ambiguous的原因和解决的文章就介绍到这了,更多相关1052 Column 'xxx' in field list is ambiguous内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
innodb_index_stats导入备份数据时报错表主键冲突的解决方法
下面小编就为大家带来一篇innodb_index_stats导入备份数据时报错表主键冲突的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-03-03Mysql中 show table status 获取表信息的方法
这篇文章主要介绍了Mysql中 show table status 获取表信息的方法的相关资料,需要的朋友可以参考下2016-03-03解决MySQL安装重装时出现could not start the service mysql error:0问题的方法
这篇文章主要为大家详细介绍了解决MySQL安装重装时出现could not start the service mysql error:0问题的方法,感兴趣的小伙伴们可以参考一下2016-06-06
最新评论