Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple joins to same table

I have this set of tables and data

CREATE TABLE item (  
    id INT PRIMARY KEY,  
    name VARCHAR  
);  
CREATE TABLE property (  
    id INT PRIMARY KEY,  
    name VARCHAR  
);  
CREATE TABLE value (  
    id INT PRIMARY KEY,  
    value VARCHAR  
);  
CREATE TABLE item_value (  
    item INT NOT NULL REFERENCES item(id),  
    property INT NOT NULL REFERENCES property(id),  
    value INT NOT NULL REFERENCES value(id)  
);  
INSERT INTO item (id, name) VALUES (1, 'item1'), (2, 'item2');  
INSERT INTO property (id, name) VALUES (1, 'prop1'), (2, 'prop2');  
INSERT INTO value (id, value) VALUES (1, 'val1'), (2, 'val2');  
INSERT INTO item_value (item, property, value) VALUES (1, 1, 1), (2,2,2 );  

I want to get a result set:

name    value_1    value_2
---------------------------  
item1   val1       <null>
item2   <null>     val2

and I use the query:

SELECT i.name as name, v1.value as value_1, v2.value as value_2 
FROM item i
LEFT JOIN item_value iv ON iv.item = i.id
LEFT JOIN value v1 ON v1.id = iv.value AND v1.id = (
  SELECT v.id FROM value v 
  JOIN property p ON p.id = iv.property 
    AND p.name = 'prop1' AND v.id = v1.id AND v.id = iv.value
)
LEFT JOIN value v2 ON v2.id = iv.value AND v2.id = (
  SELECT v.id FROM value v 
  JOIN property p ON p.id = iv.property 
    AND p.name = 'prop2' AND v.id = v2.id AND v.id = iv.value
)

The question is: How can I avoid nested selects?
I'm using Postgresql

like image 845
GetUsername Avatar asked Jan 25 '11 15:01

GetUsername


People also ask

Can I join same table twice in SQL?

As you may know, it is used to join and combine data from two or more tables into one common data set. In this article, I'm going to discuss special types of joins? in which you combine the same table twice—including joining a table to itself, also known as the self join.

Can we use two different joins in same query?

Multiple joins can be described as a query containing joins of the same or different types used more than once, thus giving them the ability to combine multiple tables.

Can we apply join on same table?

You use self-join to create a result set that joins the rows with the other rows within the same table. Because you cannot refer to the same table more than one in a query, you need to use a table alias to assign the table a different name when you use self-join.

Can you do 3 joins in SQL?

It is possible to use multiple join statements together to join more than one table at the same time. To do that you add a second INNER JOIN statement and a second ON statement to indicate the third table and the second relationship.


1 Answers

You can try with

SELECT i.name as name, v1.value as value_1, v2.value as value_2 
  FROM item i
       INNER JOIN item_value iv ON iv.item = i.id
       INNER JOIN property p ON iv.property = p.id
       LEFT JOIN value v1 ON p.name = 'prop1' AND v1.id = iv.value
       LEFT JOIn value v2 ON p.name = 'prop2' AND v2.id = iv.value
like image 146
Borja Avatar answered Nov 03 '22 00:11

Borja