title: Unknown column in 'where clause'. Neste subquery to improve perfomance of 'ORDER BY'
body: <p>I have an a relationship a little odd but must be so:</p>
  
  <pre><code>SELECT COUNT(DISTINCT `t`.`id`) 
   FROM `radcliente` `t`
   LEFT OUTER JOIN `radcliente_endereco_instalacao` `endereco_instalacao`
        ON (`endereco_instalacao`.`cliente_id`=`t`.`id`)
   LEFT OUTER JOIN `radcliente_telefone` `telefones`
        ON (`telefones`.`cliente_id`=`t`.`id`)
   LEFT OUTER JOIN `radcliente_email` `emails`
        ON (`emails`.`cliente_id`=`t`.`id`)
  LEFT OUTER JOIN `radmetodo_cobranca` `metodo_cobranca`
        ON (`metodo_cobranca`.`cliente_id`=`t`.`id`) AND (metodo_cobranca.arquivo = 'nao')
  LEFT OUTER JOIN `radacct` `ultimo_acct`
        ON (`ultimo_acct`.`username`=`t`.`login`) 
       AND (ultimo_acct.radacctid = (
           SELECT `radacctid` FROM
                    (SELECT radacctid
                       FROM `radacct` `fUzDDUDv` 
                      WHERE username = t.login
                    ) AS `fUzDDUDv` ORDER BY `radacctid` DESC LIMIT 1
                                    )
           )
  WHERE (ultimo_acct.framedipaddress = '177.23.209.194')
  </code></pre>
  
  <p>Unknown column 't.login' in 'where clause'.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Yes, that would solve a problem. I created this sub query why I'm using the Yii Framework, and the has_one relationships there is no limit, ie if one 'cliente' with millions of 'acct', the framework seeks all 'acct', to take only one this gives some GB of traffic and is very slow. To solve, I used the subquery seeking only the newest id to get the 'cliente' and the last 'acct' and make it work in Active Record, until then everything was fine, but the search was slow estremamente to seek a record took 40 seconds, then I discovered that the problem was in the 'ORDER BY' (radacctid is an index and tables are innodb), hence I did to solve the 'ORDER BY' outside the subquery, then resolvou, the problem that the subquery column the table 'cliente' (t.login) is like there, as I explained above.</p>
  
  <p>I've tried also sort by another field, eg 'acctstarttime', and continued slow, only solved when I did it this way:</p>
  
  <pre><code>SELECT `radacctid` FROM
                (SELECT radacctid
                   FROM `radacct` `fUzDDUDv` 
                  WHERE username = t.login
                ) AS `fUzDDUDv` ORDER BY `radacctid` DESC LIMIT 1
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>But the problem of INNER JOIN is that if there is no result for 'acct' does not return the 'cliente'.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The problem is not where the <code>t</code>.<code>login</code> but it is not recognized as existing within the subquery that I can not put 'out' if the order is not slow.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Read my comments? This is the situation. It really is only on the production server that has inserts and updates all the time.</p>