开发者

Select from the same table for two different values (mysql)

I am making a small forum. What i want is when listing the topics of a forum, to show the username and avatar of both of the person who made the topic and of the person who has last replied.

The current query (which currently only finds the avatar+username of the creator of topic) is:

SELECT 
    forum_topics.primkey,
    forum_topics.title,
    forum_t开发者_开发技巧opics.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 ...;

So, how can i modify the above query so to find also the username+avatar of last_reply_poster(id of the user)?

Thanks!


You have to join the users table in twice:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜