title: PHP MySQL - How can I sort paginated results by a calculated field?
body: <p>For context, though the scope is not limited to this, I'm using CakePHP 2.4.4 with it's pagination function. This queries for results (with ORDER, LIMIT and OFFSET, I believe), and once the results are returned, I calculate a field. In this case, the user enters a Postcode, and upon results being returned, the geographical distance between the search postcode and the result postcodes are individually calculated. I can't order by this, as I only have one page of results at a time.</p>
  
  <p>How would I do this manually? I imagine I could get all results in a query, then calc distance for each result, then order, then drop those not on that page, but that seems very expensive. Autotrader, for example, return hundreds of thousands of results, and they can be ordered by distance, calculated there and then.</p>
  
  <p>The maths is done in PHP on my project, but I expect this needs to be moved to within a query, but I think that makes it expensive.</p>
  
  <p>How can I order all results by a calculated field not actually stored in the database?</p>