title: MySQL duplicate column in sub-query inside join
body: <p>I have the following piece of code:</p>
  
  <pre><code>SELECT * FROM uc_redwables AS T1 
  INNER JOIN
  (SELECT dwable , MAX(time) AS time FROM uc_redwables WHERE EXISTS (SELECT * FROM `uc_following` WHERE `userid` = '5' AND find_in_set(followed, redwabledby) &gt; 0) OR find_in_set('5',`redwabledby`)
  GROUP BY dwable
  ) AS T2
  ON T1.dwable = T2.dwable AND T1.time = T2.time
  ORDER by T2.time desc LIMIT 20
  </code></pre>
  
  <p>This works as intended.</p>
  
  <p>However when I place it in my larger query:</p>
  
  <pre><code>SELECT 
  p.`id` as id, 
  p.`postinguser` as postinguser,
  p.`page` as page,
  p.`likedby` as likedby,
  p.`redwabled` as redwabled,
  case r.`redwabledby`
  when (SELECT `followed` FROM `uc_following` WHERE `userid` = '5') then r.`time`
  when '5' then r.`time`
  else p.`time`
  end as date_time 
  FROM `uc_posts` p left join 
  (
  //Code entered here
  SELECT * FROM uc_redwables AS T1 
  INNER JOIN
  (SELECT `dwable` , MAX(time) AS time FROM uc_redwables WHERE EXISTS (SELECT * FROM `uc_following` WHERE `userid` = '5' AND find_in_set(followed, redwabledby) &gt; 0) OR find_in_set('5',`redwabledby`)
  GROUP BY `dwable`
  ) AS T2
  ON T1.dwable = T2.dwable AND T1.time = T2.time
  ORDER by T2.time desc LIMIT 20
  //Code entered ends here
  ) r on p.id = r.dwable
  WHERE p.`postinguser` = '5' OR find_in_set('5',p.`redwabled`) OR EXISTS (SELECT * FROM `uc_users` WHERE `id` = '5' AND find_in_set(postinguser, following) &gt; 0) OR EXISTS (SELECT * FROM `uc_following` WHERE `userid` = '5' AND find_in_set(followed, redwabled) &gt; 0) 
  order by date_time desc LIMIT 20
  </code></pre>
  
  <p>I get a <code>Duplicate column name 'dwable'</code> error.</p>
  
  <p>I would assume this is because it's used elsewhere. I have looked up a number of different solutions including using aliases but I can't seem to get anything to work. </p>
  
  <p>Can anyone help?</p>
  
  <p>Thanks</p>