数据库报错:Column ‘xxx‘ in field list is ambiguous
文章目录
- 一、报错场景
- 二、解决方法
一、报错场景
在sql中执行sql查询后报错
SELECT source_code, name
FROM table1
JOIN table2 ON table1.id = table2.id;
在SQL查询中,当你遇到“Column ‘source_code’ in field list is ambiguous”这个错误时,意味着在查询中引用了多个表,而这两个表都包含名为source_code的列,而没有明确指定该列来自哪个表
。为了解决这个问题,你需要使用表名或表的别名来明确指定该列属于哪个表。
二、解决方法
使用表名或别名前缀:在引用列名时,加上表名或表的别名。例如,如果你有两个表table1和table2,它们都有source_code这一列,你可以这样写:
SELECT table1.source_code, table2.source_code
FROM table1
JOIN table2 ON table1.id = table2.id;