内部联接切换到外部,无效的标识符

时间:2014-04-23 17:49:59

标签: oracle inner-join outer-join

所以我正在玩几个连接查询,我注意到一些不寻常的行为,当我将查询从内部连接切换到外部连接,并将其他所有内容保持不变时,我收到错误,下面是两个查询正在运行。

SELECT Staff.Fname AS "First Name", Staff.Lname AS "Second Name", Booking_ID
FROM Staff INNER JOIN Booking ON Staff.StaffID = Booking.StaffID;

A screenshot of an SQL Query returning successfully

然后当我切换它时,

SELECT Staff.Fname AS "First Name", Staff.Lname AS "Second Name", Booking_ID
FROM Staff OUTER JOIN Booking ON Staff.StaffID = Booking.StaffID;

我得到Error ORA-00904 Invalid identifier "Staff"."StaffID"

我认为上述查询应该返回已预订的所有工作人员以及所有尚未预订的人员。

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

问题是您需要说明LEFT OUTER JOIN(或FULL或RIGHT)而不是OUTER JOIN。 Oracle将SQL中的OUTER视为表别名。也就是说,它正在读作:

SELECT Staff.Fname AS "First Name", Staff.Lname AS "Second Name", Booking_ID
  FROM Staff foo 
        JOIN Booking ON Staff.StaffID = Booking.StaffID;

虽然看起来很奇怪,但这确实有效:

SELECT OUTER.Fname AS "First Name", OUTER.Lname AS "Second Name", Booking_ID
  FROM Staff OUTER JOIN Booking ON OUTER.StaffID = Booking.StaffID;

Oracle会将此视为STAFF表(别名为OUTER)和BOOKING之间的简单内部联接。

基本上,OUTER关键字,而不是Oracle中的保留字。也就是说,它具有特殊含义,但仍可用作别名。