Red Red - 11 months ago 33
SQL Question

Join table twice - on two different columns of the same table

I have a very confusing database with a table that holds two values I need in a separate table. Here is my issue:

- id

- id
- table1_id
- table3_id_1
- table3_id_2

- id
- value

I need to go from table1 and do a join that would give me back the value from
in two separate columns. So I want something like this: | | table2.table3_id_1 | table2.table3_id_2 | X | Y

are the values for the row connected by

Possibly make them variables or something so I can filter them in a
clause as well?

Answer Source
SELECT t2.table1_id
     ,          AS table2_id
     , t2.table3_id_1
     , t2.table3_id_2
     , t31.value      AS x
     , t32.value      AS y
FROM   table2 t2
LEFT   JOIN table3 t31 ON = t2.table3_id_1
LEFT   JOIN table3 t32 ON = t2.table3_id_2;

There is no need to join in table1. table2 has all you need - assuming there is a foreign key constraint guaranteeing referential integrity (all t2.table1_id are actually present in table1). Else you may want to join to table1, thereby selecting only rows present in table1.

I use LEFT [OUTER] JOIN (and not [INNER] JOIN) to join to both instances of table3 for a similar reason: it is unclear whether referential integrity is guaranteed - ans whether any of the key columns can be NULL. An [INNER] JOIN would drop rows from the result where no match is found. I assume you would rather display such rows with a NULL value for any missing x or y.

And needs to be UNIQUE, or we might multiply rows with several matches from each LEFT JOIN: