2011-05-15 181 views
1

我在製作一個小論壇。我想要的是在列出論壇主題時,顯示發起主題的人員和最後回覆人員的用戶名和頭像。從同一個表中選擇兩個不同的值(mysql)

當前查詢(目前僅發現主題的創作者的頭像+用戶名)是:

SELECT 
    forum_topics.primkey, 
    forum_topics.title, 
    forum_topics.creatorid, 
    forum_topics.last_reply_poster, 
    users.username, 
    users.avatar, 
    forum_tracks.lastmark 
FROM 
    users, 
    forum_topics 
    LEFT JOIN forum_tracks 
     ON forum_tracks.userid='".$_SESSION['loggeduserkey']."' 
     AND forum_tracks.topic_id=forum_topics.primkey 
WHERE 
    forum_topics.cat_id='".$forum_id."' 
    AND users.userkey=forum_topics.creatorid 
ORDER BY ...; 

所以,我怎麼能修改上面的查詢,以便還找到last_reply_poster用戶名+頭像(用戶的ID)?

謝謝!

回答

1

你必須加入users表中兩次:

SELECT 
    forum_topics.primkey, 
    forum_topics.title, 
    forum_topics.creatorid, 
    forum_topics.last_reply_poster, 
    users.username, 
    users.avatar, 
    u2.username, 
    u2.avatar, 
    forum_tracks.lastmark 
FROM 
    users, 
    users as u2, 
    forum_topics 
    LEFT JOIN forum_tracks 
     ON forum_tracks.userid='".$_SESSION['loggeduserkey']."' 
     AND forum_tracks.topic_id=forum_topics.primkey 
WHERE 
    forum_topics.cat_id='".$forum_id."' 
    AND users.userkey=forum_topics.creatorid 
    AND u2.userkey=forum_topics.last_reply_poster 
ORDER BY ...; 
+0

它的工作原理。非常感謝你! :) – 2011-05-15 16:09:20

相關問題