title: Using Complex Expressions in "Order By" in Mysql
body: <p>I have a table "Deal" </p>
  
  <pre><code>+---------+---------+
  | deal_id | expired |
  +---------+---------+
  |       1 |       0 |
  |       2 |       0 |
  |       3 |       0 |
  |       4 |       0 |
  |       5 |       1 |
  |       6 |       0 |
  |       7 |       1 |
  |       8 |       1 |
  |       9 |       0 |
  |      10 |       0 |
  +---------+---------+
  </code></pre>
  
  <p>I would like to archive the following order:</p>
  
  <p>1) Expired Deals at the bottom</p>
  
  <p>2) Deals with deal_id higher then 5 at the top, ordered by deal_id</p>
  
  <p>3) Deals with id lower/equal 5 at the bottom ordered by RAND with seed </p>
  
  <hr>
  
  <p>SELECT deal_id, expired FROM Deal ORDER by expired = 1, deal_id &lt; 5, rand(1) desc</p>
  
  <p>This query is wrong as the top of the table will be also ordered by rand, and the top part I would like to order by deal_id desc.</p>
  
  <hr>
  
  <p>This is how it should look after: </p>
  
  <pre><code>+---------+---------+
  | deal_id | expired |
  +---------+---------+
  |       10|       0 | top part ordered by
  |       9 |       0 | deal_id desc 
  |       6 |_______0 | if ( deal_id &lt; 5 AND expired = 0 )
  |       4 |       0 |  
  |       1 |       0 | bottom part ordered
  |       5 |       0 | by rand(seed) 
  |       7 |       0 | expired = 1 at the bottom
  |       5 |       1 |
  |       7 |       1 |
  |       8 |       1 |
  +---------+---------+
  </code></pre>
  
  <p>Is it possible to archive that by using just expressions in "ORDER BY"? <strong>I know I could use UNION, but I really don't want to</strong>, It will let me keep things simple in my framework.</p>
  
  <p>Thanks.</p>