我正在建立一个小论坛。我想要的是在列出论坛的主题时,显示制作该主题的人和最后一个回复的人的用户名和头像。
当前查询(目前仅查找主题创建者的头像+用户名)为:
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)?
谢谢!
答案 0 :(得分:2)
您必须两次加入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 ...;