Oracle 11g"重复列名称"没有重复列的视图错误

时间:2017-11-19 01:59:00

标签: sql oracle11g

不知何故,Oracle 11g认为我的视图创建代码中有重复的列名:

DROP VIEW users_view;
CREATE VIEW users_view AS 
    SELECT * FROM users u
        INNER JOIN states s 
            ON u.state_id = s.state_id
        INNER JOIN accounts a
            ON u.account_id = a.account_id
;
/

我在SQLDeveloper中运行它并收到此错误消息:

Error starting at line : 109 in command -
CREATE VIEW users_view AS 
    SELECT * FROM users u
        INNER JOIN states s 
            ON u.state_id = s.state_id
        INNER JOIN accounts a
            ON u.account_id = a.account_id
Error report -
ORA-00957: duplicate column name
00957. 00000 -  "duplicate column name"
*Cause:    
*Action:

我可以解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

显然,此查询有重复的列:

SELECT *
FROM users u INNER JOIN
     states s 
     ON u.state_id = s.state_id INNER JOIN
     accounts a
     ON u.account_id = a.account_id

重复项包括u.state_ids.state_idu.account_ida.account_id - 表别名将被忽略。

我建议您列出每个表中所需的所有列。但是,如果重复项只是JOIN个键,则会有一个快捷方式 - 您可以使用USING代替ON

SELECT *
FROM users u INNER JOIN
     states s 
     USING (state_id) INNER JOIN
     accounts a
     USING (account_id)