简体   繁体   中英

SQL JOIN and LEFT OUTER JOIN

I've been having trouble understanding this example, I have the answer but I'm having trouble wrapping my head around how this works. How are the joins working exactly?

Examine the structures of the PLAYER and TEAM tables:

    PLAYER
    -------------
    PLAYER_ID NUMBER(9) PK
    LAST_NAME VARCHAR2(25)
    FIRST_NAME VARCHAR2(25)
    TEAM_ID NUMBER
    MANAGER_ID NUMBER(9)

    TEAM
    ----------
    TEAM_ID NUMBER PK
    TEAM_NAME VARCHAR2(30)

For this example, team managers are also players, and the MANAGER_ID column references the PLAYER_ID column. For players who are managers, MANAGER_ID is NULL .

Which SELECT statement will provide a list of all players, including the player's name, the team name, and the player's manager's name?

ANSWER:

    SELECT p.last_name, p.first_name, m.last_name, m.first_name, t.team_name
    FROM player p
    LEFT OUTER JOIN player m ON (p.manager_id = m.player_id)
    LEFT OUTER JOIN team t ON (p.team_id = t.team_id);

In your example the players that are also managers, have a null value in the manager_id column. Take the first part of the SQL statement, which is:

SELECT p.last_name, p.first_name, m.last_name, m.first_name, t.team_name
FROM player p
LEFT OUTER JOIN player m ON (p.manager_id = m.player_id)

Since you are performing a LEFT JOIN (instead of an INNER JOIN), your manager records will not be filtered out (even though their NULL value in the manager_id column, will not successfully join to any player_id in the player table), and m.last_name and m.first_name will be NULL for those manager players.

If you were to change that query to an INNER JOIN instead, your players that are also managers would not be returned at all by the query, because they have a NULL value for manager_id and there is no player records with a player_id of NULL.

SELECT p.last_name, p.first_name, m.last_name, m.first_name, t.team_name
FROM player p
INNER JOIN player m ON (p.manager_id = m.player_id)

I hope this helps!

So the first LEFT OUTER JOIN takes the player table, then adds on the info for each players manager. Each player has an ID for its manager, who is also a player with an ID . If Mr. A, with player_id 9 , is a manager for Ted, with player_id 5 , then Ted's manager_id will be 9 . The first join takes Ted's manager_id , 9 , and matches it to the player_id of his manager, Mr. A, so that the manager's info is now on the table as well, and m.last_name and m.first_name will show Mr. A's name. The second join takes the team_id and simply matches it to the table of teams, appending the team_name to the player's information in the table.

It's tough to explain without sample data and diagrams. Sorry.

An easier way to think about it is to look at each of the joins individually.

A Left Outer Join is a type of Join that joins two tables together on the provided pairings, and if a match exists, data is pulled from the right-hand side. But if no match is found, the data returned from the right is simply Null . This is different from an Inner Join where if no match is found, the records are simply omitted.

For this, a player may or may not have a manager, so it's using a Left Outer Join to either pull back the manager's Player record if the player has a manager, or if the player does not, it will return Null .

Similarly, a player may or may not be on a team, so it is Left Outer Join ing to the Team table to pull back the Team information if the player is on a team, otherwise it will be Null .

The Select statement simply chooses which fields it wants to display from the three tables.

A good example of the different types of Join s can be seen here: Difference Between Inner and Outer Joins

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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