CoffeeAddict Posted November 24, 2012 Share Posted November 24, 2012 (edited) I have a project I'm working on and I'm stuck on how to order results closest to a specific number. I have a breed of dog with an ideal number. We will say 250 For that breed I have 5 different dogs all that have their own set of numbers. I then added those together and now have a single total for each of those 5 dogs. Out of those 5 numbers, I need to order them by the closest to 250. Now some may be below 250 or above 250. Lets say my totals for these 5 dogs were 240 310 122 109 87 That right there is how I would want them ordered. Because 240 is closest to 250 and so on. I know how to order by ascending or desceding, but not how to order according to a specific number. Help please? Edited November 24, 2012 by CoffeeAddict Quote Link to comment https://forums.phpfreaks.com/topic/271093-find-the-total-closest-to-specific-number/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 24, 2012 Share Posted November 24, 2012 You would ORDER BY the absolute value ABS() of the difference between the target number and the summed number for each entry. Quote Link to comment https://forums.phpfreaks.com/topic/271093-find-the-total-closest-to-specific-number/#findComment-1394701 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.