SQL ?JOIN? Query

Results 1 to 2 of 2

Thread: SQL ?JOIN? Query

  1. #1
    Join Date
    Dec 1969

    Default SQL ?JOIN? Query

    In my Forum site, I am trying to obtain the username of the user that Started the Topic, and &#039;in the same SQL statement&#039;, the username of the person that last posted in that same topic.<BR><BR>My Topics table has the Foriegn Keys &#039;TopicUserID&#039; & &#039;LastPostUserID&#039;.<BR><BR>My Users table has the fields &#039;UserID&#039; & &#039;Username&#039;.<BR><BR>The following SQL will obviously return the UserID AND Username of the person that started the topic, however, I don&#039;t know how to also get the Username of T.LastPostUserID in this same statement.<BR><BR>SELECT T.LastPostUserID, T.TopicUserID, U.Username AS StartedByUsername<BR>FROM Topics T INNER JOIN Users U ON T.TopicUserID = U.UserID<BR><BR>If anyone could shed any light on how best to achieve the joins for both StartedByUsername AND LastPostUsername I would be most grateful!<BR><BR>Thanks<BR>

  2. #2
    Join Date
    Dec 1969

    Default Have to join *twice* ...

    ...to the USERS table:<BR><BR>SELECT ST.UserName AS StartedByName, LP.UserName AS LastPostName<BR>FROM Topics AS T, Users AS ST, Users AS LP<BR>WHERE ST.userID = T.TopicUserID<BR>AND LP.userID = T.LastPostUserID<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts