TransWikia.com

How to perform multiple joins onto a table and then join resulting table onto multiple columns into another table

Database Administrators Asked by cubesnyc on December 19, 2021

|  family_id  |  parent_id_1  |  parent_id_2  | 
-----------------------------------------------
|     1234    |      9999     |      9567     | 
-----------------------------------------------


|  person_id|    address_id |    name    |    job _id    |    
-----------------------------------------------------------
|   1234    |   3           |   ABCD     |    5          |
-----------------------------------------------------------
|   9999    |   4           |   ABCD     |    3          | 
-----------------------------------------------------------


|  job_id  |  title |  start_date | 
-----------------------------------------------
|     3    |    asdf|     3-2-2001| 
-----------------------------------------------

|  address_id  |  street |  zip    | 
-----------------------------------------------
|     3        | 1 main  |   11234 | 
-----------------------------------------------

I have copy and pasted a database schema from a similar question and made some minor modifications. In mysql I have several tables: Family, Person, Address, Job. Family has two columns that reference a person. A person has a column that references an Address, and one column that references a job. I would like query a family so that I can see both persons with their respective jobs and addresses. What is the correct way to accomplish that?

2 Answers

Parentheses are allowed:

FROM ( a JOIN b ON ... )
JOIN ( c JOIN d ON ... ) ON ...

(etc)

This can be especially useful when using RIGHT JOIN or LEFT JOIN -- it will help clarify your intention without having to figure out the "precedence" rules.

Answered by Rick James on December 19, 2021

You have would have to join twice:

SELECT
  fam.family_id
 ,fam.parent_id_1
 ,parent1.address_id AS parent_1_address_id
 ,parent1.name AS parent_1_name
 ,parent1.job_id AS parent_1_job_id
 ,fam.parent_id_2
 ,parent2.address_id AS parent_2_address_id
 ,parent2.name AS parent_2_name
 ,parent2.job_id AS parent_2_job_id
FROM
  family fam
LEFT JOIN
  person parent1
    ON parent1.person_id = fam.parent_id_1
LEFT JOIN
  person parent2
    ON parent2.person_id = fam.parent_id_2
LEFT JOIN
  job job1
    ON parent1.job_id = job1.job_id
<... over and over ... >

You can use a common table expression to wrap some of the identical code together, but it's the same steps ultimately:

WITH person_detail AS
(
  SELECT
    person.person_id
   ,person.name
   ,<job columns>
   ,<address columns>
  FROM
    person person
  LEFT JOIN
    job job
      ON job.job_id = person.job_id
  LEFT JOIN
    address address
     ON address.address_id = person.address_id
)
SELECT
  fam.family_id
 ,fam.parent_id_1
 ,<parent_1 columns>
 ,fam.parent_id_2
 ,<parent_2 columns>
FROM
  family fam
LEFT JOIN
  person_detail parent1
    ON parent1.person_id = fam.parent_id_1
LEFT JOIN
  person_detail parent2
    ON parent2.person_id = fam.parent_id_2

Answered by bbaird on December 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP