简体   繁体   English

连接具有相同列名的 2 个表时出现不明确的列错误

[英]ambiguous column error when joining 2 tables which have the same column name

in my table A {ID, FID, Name, Age}在我的表 A {ID, FID, Name, Age}
in my table B{ID, Job}在我的表 B{ID, Job}

When I use当我使用

SELECT ID // I want to get the ID of table B //**error**//
FROM A TA JOIN B TB
ON TA.ID = TB.FID
ORDER BY TA.Name;

Error : ambiguous column name ID错误:不明确的列名 ID

Is there anyway to do that without having to rename the column name?有没有办法做到这一点而不必重命名列名? (The Worst scenario) (最坏的情况)
Thank you谢谢

SELECT TB.ID
FROM A TA JOIN B TB
ON TA.ID = TB.FID
ORDER BY TA.Name;

Use the full name (table name and column name seperated by a dot) to specifically select a column.使用全名(表名和列名用点隔开)具体到 select 一个列。

Your query doesn't fit your described table structure, I guess you swapped table A with table B.您的查询不符合您描述的表结构,我猜您将表 A 与表 B 交换。

SELECT TB.ID  ...

You need to prefix any ambiguous column anywhere with your table alias.您需要在任何地方使用表别名为任何不明确的列添加前缀。

you don't need to rename anything.你不需要重命名任何东西。 you just need to specify the source of ambigous field in SELECT clause since such field exists in both sources (tables)您只需要在 SELECT 子句中指定不明确字段的来源,因为这样的字段存在于两个来源(表)中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM