Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

Table1
- id

Table2
- id
- table1_id
- table3_id_1
- table3_id_2

Table3
- id
- value

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

table1.id | table2.id | table2.table3_id_1 | table2.table3_id_2 | X | Y

Where X and Y are the values for the row connected by table3_id_1 and table3_id_2 respectively.

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

like image 595
Red Avatar asked May 22 '12 21:05

Red


People also ask

How do I join two different columns in the same table in SQL?

The following example shows how to concatenate three different columns: (SELECT id, email1 AS email FROM customer) UNION (SELECT id, email2 AS email FROM customer) UNION (SELECT id, email3 AS email FROM customer) ORDER BY id, email; As you can see, it's important that all the queries return the same columns.

Can you join same table twice?

To form a self-join, you specify the same table twice with different table aliases and provide the join predicate after the ON keyword. In this syntax, the table_name is joined to itself using the INNER JOIN clause.

Can you join two tables with different columns?

Merging tables by columns. Multiple tables can be merged by columns in SQL using joins. Joins merge two tables based on the specified columns (generally, the primary key of one table and a foreign key of the other).

Can you use LEFT join twice?

Yes, indeed! You can use multiple LEFT JOINs in one query if needed for your analysis.


2 Answers

SELECT t2.table1_id
     , t2.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 t31.id = t2.table3_id_1
LEFT   JOIN table3 t32 ON t32.id = t2.table3_id_2;

There is no need to involve 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 also 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 - and 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.

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

  • Two SQL LEFT JOINS produce incorrect result
like image 59
Erwin Brandstetter Avatar answered Sep 28 '22 14:09

Erwin Brandstetter


If you join a table several times, use aliases to distinguish them:

SELECT table1.id,table2.id,table2.table3_id_1,table2.table3_id_2,t3_1.id,t3_2.id
FROM table1
JOIN table2 ON table1.id=table2.table1_id
JOIN table3 t3_1 ON table2.table3_id_1=t3_1.id
JOIN table3 t3_2 ON table2.table3_id_2=t3_2.id
WHERE ... t3_1.id=... AND ... t3_2.id=...
like image 35
nosid Avatar answered Sep 28 '22 14:09

nosid