Domanda

So I was playing around with a few join queries, and I noticed some unusual behaviour, when I switch my query from an inner to an outer join, and leave everything else the same, I get an error, below are the two queries running.

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

And then when I switch it around,

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

I get Error ORA-00904 Invalid identifier "Staff"."StaffID".

I thought the above query should return all staff who have made a booking along with the bookingid, as well as all those who haven't made a booking.

Any ideas?

È stato utile?

Soluzione

The problem is that you need to state LEFT OUTER JOIN (or FULL or RIGHT) instead of OUTER JOIN. Oracle is seeing OUTER in your SQL as a table alias. That is, it's reading it as:

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

Although it would look strange, this would actually work:

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

Oracle would see this as a simple inner join between the STAFF table (aliased as OUTER), and BOOKING.

Essentially, OUTER is a keyword but not a reserved word in Oracle. That is, it has special meaning but can still be used as an alias.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top