MySQL Order By Numeric Difference
Like this blog? Consider exploring one of our sponsored banner ads...
A nice way I found to use MySQL to order a result set by the absolute value of the numeric difference between two numbers, where one is the value in a column. I had never used SQL to subtract two numbers, let alone take the absolute value and use that to order the results. You could use something like this to show numbers that are most closely related, maybe in a zipcode application or a priority scoring application.
SELECT * FROM `table` WHERE `something`=`whatever` ORDER BY ABS ( 711 - `num` )
About this entry
You’re currently reading “MySQL Order By Numeric Difference,” an entry on BRADINO
- Published:
- 11.9.07 / 6pm
- Category:
- MySQL
- Tags:
3 Comments
Jump to comment form | comments rss [?]