SQL-列名重复

SQL-Duplicate column names

我正在使用 SQL 开发人员,我正在尝试对两个 table 进行外部连接。它显示的错误是 "Duplicate column names"。我在比较时使用了 table 名称,但它仍然出错。代码如下:

CREATE VIEW OECD_VIEW AS
SELECT * FROM DM_OECD_GDP FULL OUTER JOIN DM_OECD_DOCTORS 
ON DM_OECD_GDP.DATA_YEAR = DM_OECD_DOCTORS.DATA_YEAR;

我听说这个错误可以通过别名来解决,但我不知道如何在比较时使用别名。这能做到吗?

谢谢

您必须显式命名来自两个表的返回字段和具有相同名称的别名字段,例如:

CREATE VIEW OECD_VIEW AS
SELECT DM_OECD_GDP.DATA_YEAR AS GDP_DATA_YEAR,
       DM_OECD_DOCTORS.DATA_YEAR AS DOC_DATA_YEAR,
       ... rest of the fields here
FROM DM_OECD_GDP 
FULL OUTER JOIN DM_OECD_DOCTORS 
ON DM_OECD_GDP.DATA_YEAR = DM_OECD_DOCTORS.DATA_YEAR;

这是创建具有别名和完全外部连接的视图的通用查询:

create view v6 as select t1.c1, t2.c2 from t1 FULL OUTER JOIN t2 on t1.id = t2.id;

如果两个表有相同的列名,可能是外键,不要使用 * ,手动 select 列名

table_one - ID,Name
table_two - ID,SecondName

select table_one.ID,table_one.Name,table_two.ID as ID2,
  table_two.SecondName full outer join on table_one.ID=table_two.ID