Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

SQL join when tables are separated by one or more other tables

Say in an SQL database I have an "Employee" table, which points to a "Person" table, which points to a Person_Name table:

Employee (table)
+------------+-----+      Person (table)
| Field      | Key |      +------------+-----+      Person_Name (table)
+------------+-----+      | Field      | Key |      +------------+-----+
| employee_id| PK  |      +------------+-----+      | Field      | Key |
| person_id  | FK  | -->  | person_id  | PK  |      +------------+-----+
+------------+-----+      | name_id    | FK  | -->  | name_id    | PK  |
                          +------------+-----+      | people_id  | FK  |
                                                    | name       |     |
                                                    +------------+-----+

Open in new window

What query would list me all employees and their names?
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deleyd

ASKER

Thank you for the query. I seem to get no result from it. I created a test database and filled it in as follows:
Employee (table)              Person (table)               Person_Name (table)
+------------+---------+      +------------+--------+      +----------+------+
| employee_id|person_id|      | person_id  | name_id|      | name_id  | name |
+------------+---------+      +------------+--------+      +----------+------+
|     1      |   10    |      |    10      |  100   |      |   100    | One  |
|     2      |   20    | -->  |    20      |  200   | -->  |   200    | Two  |
|     3      |   30    |      |    30      |  300   |      |   300    | Three|
+------------+---------+      +------------+--------+      +----------+------+

Open in new window

Then run the query, which doesn't complain, but also gives me no results. Hmm..,
٩(͡๏̯͡๏)۶
(I also removed the people_id from the Person_Name table which served no purpose.)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deleyd

ASKER

Thank you for your help. I'm stumped on this one, why I'm getting no results, so I made some screen shots and attached them here.  Hopefully  someone can spot the problem.
Person Database
User generated imageName Database
User generated imageEmployee Database
User generated imageQuery2 Results
User generated imageQuery1 Results
User generated image
Avatar of deleyd

ASKER

Oh I see the problem.
Avatar of deleyd

ASKER

OK Thank you I'll use the Image button next time.

Both queries work now.

So I see one pattern is:
SELECT
FROM
JOIN
JOIN
ON
ON

and the other pattern is:
SELECT
FROM
JOIN ... ON ...
JOIN ... ON ...

I suppose both work fine. Thank you for your help!
So, name_id wasn't equal :)
Machine is always right ;)
Avatar of deleyd

ASKER

(I have a T-shirt with a human working at the computer. Human thinks, "Stupid computer." Computer thinks, "Stupid human.")

:)