MySQL Order By Numeric Difference
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.
SQL:
-
SELECT * FROM `table`
-
WHERE `something`=`whatever`
-
ORDER BY ABS ( 711 - `num` )
Home | MySQL | MySQL Order By Numeric Difference
You’re currently reading “ MySQL Order By Numeric Difference ,” an entry on BRADINO
- Published:
- 11.9.07 / 6pm
- Category:
- MySQL












