2010-05-24 7 views
0

J'ai 3 tables (suit, les affichages, les utilisateurs)Combine instruction SQL

suit a 2 champs -> PROFILE_ID, following_id

messages a 3 champs -> post_id, PROFILE_ID, contenu

utilisateurs a 3 champs -> PROFILE_ID, prenom, Last_Name

J'ai une valeur de 1 follows.profile_id que je veux faire correspondre.

Quand je lance l'instruction SQL ci-dessous je reçois la 1ère étape pour obtenir les données correctes. Cependant, je veux maintenant faire correspondre le postings.profile_id de cet ensemble résultant à la table users de sorte que chacun des noms (prénom et nom) soit également affiché pour tous les messages listés.

Nous vous remercions de votre aide! :)

Ex:

SELECT * 
    FROM follows 
    JOIN postings ON follows.following_id = postings.profile_id 
    WHERE follows.profile_id = 1 

Répondre

1

utilisation:

SELECT * 
    FROM follows 
    JOIN postings ON follows.following_id = postings.profile_id 
    INNER JOIN users on postings.profile_id = users.profile_id 
    WHERE follows.profile_id = 1 
ORDER BY tweets.modified DESC 
+0

Merci beaucoup, facile et simple à utiliser! :) – ninu

1

Utilisation:

SELECT p.*, 
     u.first_name, 
     u.last_name 
    FROM POSTINGS p 
    JOIN FOLLOWS f ON f.following_id = p.profile_id 
        AND f.profile_id = 1 
    JOIN USERS u ON u.profile_id = p.profile_id 
ORDER BY tweets.modified DESC 
1
SELECT * 
FROM follows, postings, users 
WHERE follows.following_id = postings.profile_id 
AND users.profile_id = follows.profile_id 
AND follows.profile_id = 1 
ORDER BY tweets.modified DESC 
1

semble assez facile d'étendre le rejoindre à une autre table ... peut-être je manque une partie de la question!

select 
     f.*, 
     p.*, 
     u.* 
    from 

     follows f 

      inner join 
     postings p 
      on f.following_id = p.profile_id 

      inner join 
     users u 
      on p.profile_id = u.profile_id 


    where follows.profile_id = 1