title: writing mysql query with zend framework
body: <p>i have this query that works fine </p>
  
  <pre><code>SELECT t.username
  FROM users t LEFT JOIN friends y ON t.id=y.user_id2 and y.user_id1=2
  WHERE LOWER(t.username) LIKE 'ha%'
  ORDER BY 
      CASE WHEN y.user_id2 IS NULL THEN 1 
      ELSE 0 
      END     
      ,t.username;
  </code></pre>
  
  <p>i'm trying to write it with zend framework and this is what i came up with </p>
  
  <pre><code>        $users = new Users;
          $select = $users-&gt;select();
          $select-&gt;setIntegrityCheck(false);
          $select-&gt;from(array('t1' =&gt; 'users'), array('username'));
          $select-&gt;joinLeft(array('t2' =&gt; 'friends'), 't1.id=t2.user_id2 and t2.user_id1 =2');
          $select-&gt;where("LOWER(t1.username) like '$input%'");
          $select-&gt;order("t1.username, CASE WHEN t2.user_id2 IS NULL THEN 1 ELSE 0 END ");
          $listofusernames = $users-&gt;fetchAll($select);
  </code></pre>
  
  <p>however it seems not to work and i get this error </p>
  
  <pre><code>Fatal error: Uncaught exception 'Zend_Db_Statement_Mysqli_Exception' with message 'Mysqli prepare error: Unknown column 't1.username, CASE WHEN t2.user_id2 IS NULL THEN 1 ELSE 0 END ' in 'order clause'' in /opt/lampp/htdocs/vote_old/library/Zend/Db/Statement/Mysqli.php:77 Stack trace: #0
  </code></pre>
  
  <p>apparently it has to do with the case embedded in the order by clause.</p>
  
  <p>do you have any idea how to fix that code ?</p>
  
  <p>thank you </p>