Join two tables with an inner join and two foreign keys

I have two tables:

Table Utenti:

Table utenti

Social table:

Table Social

I want this result:

Federica - Luca 0.1
Federica - Vincenzo 0.6
Federica - Silvia 0.3
...
Silvia - Vincenzo 0.5

      

How do I perform an inner join between two tables, restoring both usernames?

I've tried this:

SELECT * 
FROM   Utenti 
INNER  JOIN Social 
         ON Utenti.ID_UT = Social.ID_UT1 
           AND Utenti.ID_UT = Social.ID_UT2

      

+3


source to share


2 answers


Close ... use the social feature as a starting point and join the Utenti table twice, one for each id.

 SELECT u1.username, u2.username,social.val
 FROM social 
 INNER JOIN utenti u1 ON U1.ID_UT=Social.ID_UT1
 inner join utenti u2 ON U2.ID_UT=Social.ID_UT2

      



You can join the same table multiple times if you provide an alias (here u1 and u2).

+5


source


SELECT u1.Username, u2.Username, s.val

      



FROM Social s JOIN U1 ON u1.ID_UT = s.ID_UT1 JOIN Utenti u2 ON u2.ID_UT = s.ID_UT2

0


source







All Articles