title: Resolve data for 2 tables
body: <p>Let's say, I have 3 tables. "users", "topics" and "replies.</p>
  
  <p>The "topics" and "replies" table do have a column "user_id" which references to "id" on the "users" table. The "replies" table also have a column "topic_id" which references "id" on "topics". Also imagine that each topic has 1 reply.</p>
  
  <p>Now, I want to fetch all topics and resolve the username for both tables, so the output should include a "topic_username" and a "reply_username".</p>
  
  <p>I know how to select the username for the topic, but how can I do that for both the topics and replies table?</p>
  
  <p>Thanks</p>
  
  <p>Updated code:</p>
  
  <pre><code>SELECT
     t.*,
     t_users.username as topic_user,
     last_reply.user_id as last_reply_user_id,
     r_users.username as last_reply_username,
     last_reply.replies_count,
  
  ORDER BY IFNULL(last_reply.created_at,t.created_at DESC
  
  FROM
  
     topics as t
     left join users as t_users on t_users.id = t.user_id
     left join ( select r.*,count(r.id) as replies_count from (select * from replies order by id desc) as r group by r.topic_id ) as last_reply on last_reply.topic_id = t.id
     left join users as r_users on r_users.id = last_reply.user_id
  </code></pre>