title: Conditional Order by clause in mysql
body: <p>I have read some posts having the same problem. I tried to adapt their answers but not succeeded.
  I want to sort columns by status than as per status further sort the columns.
  May the below query will clear my point</p>
  
  <pre><code>SELECT a.*,STR_TO_DATE(d_date, "%m/%d/%Y")&gt;CURDATE() as status
  FROM table AS a ORDER BY status DESC, 
  IF(status=0,
  'DATEDIFF(STR_TO_DATE( a.d_date, "%m/%d/%Y" ),CURDATE()) DESC',
  'DATEDIFF(STR_TO_DATE( a.d_date, "%m/%d/%Y" ),CURDATE()) ASC')
  </code></pre>
  
  <p>UPDATE:
  After below query</p>
  
  <pre><code>SELECT a . * , STR_TO_DATE( d_date,  "%m/%d/%Y" ) &gt; CURDATE( ) AS 
  STATUS 
  FROM  `table` AS a
  ORDER BY STATUS DESC , IF( 
  STATUS =0, DATEDIFF( STR_TO_DATE( a.d_date,  "%m/%d/%Y" ) , CURDATE( ) ) DESC , DATEDIFF( STR_TO_DATE( a.d_date, "%m/%d/%Y" ) , CURDATE( ) ) ASC ) 
  LIMIT 0 , 30
  </code></pre>
  
  <p>I've got the below error</p>
  
  <pre><code>#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC, DATEDIFF(STR_TO_DATE( a.d_date, "%m/%d/%Y" ),CURDATE()) ASC) LIMIT ' at line 4
  </code></pre>
  
  <p>If my table name is <code>table</code> than For this recored </p>
  
  <pre><code>d_date
  06/28/2012
  06/23/2012
  06/20/2012
  06/19/2012
  </code></pre>
  
  <p>if current date is <code>06/21/2012</code></p>
  
  <p>the output should be</p>
  
  <pre><code>d_date               status
  06/23/2012              1
  06/28/2012              1
  06/20/2012              0
  06/19/2012              0
  </code></pre>
  
  <p>May this information is enough. Please let me know if it's not clear yet.</p>
  
  <p>Thanks</p>