_spaz Posted September 23, 2009 Share Posted September 23, 2009 Is it possible to query a result and search plus/minus the value? example: say my value is "10" how do I get it to include results +/- 5 so that I get "5 6 7 8 9 10 11 12 13 14 15" Not sure if its possible and I can't find my answer anywhere..... thx. Quote Link to comment https://forums.phpfreaks.com/topic/175291-solved-plusminus-a-value/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 23, 2009 Share Posted September 23, 2009 Take the absolute value of the difference between the desired value (10 in your example) and the value in the table. Then return those rows with a result that is less-than or equal to 5. Quote Link to comment https://forums.phpfreaks.com/topic/175291-solved-plusminus-a-value/#findComment-923846 Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2009 Share Posted September 24, 2009 Query to accomplish the above statement - $query = "SELECT your_column, ABS(10-your_column) as dif FROM your_table HAVING dif <= 5"; Quote Link to comment https://forums.phpfreaks.com/topic/175291-solved-plusminus-a-value/#findComment-923849 Share on other sites More sharing options...
corbin Posted September 24, 2009 Share Posted September 24, 2009 If you did the processing in PHP, you could have it such that indexes were still usable: SELECT * FROM blah WHERE field BETWEEN 5 AND 15; Edit: Hrmmm.... Or maybe I misunderstood something.... Quote Link to comment https://forums.phpfreaks.com/topic/175291-solved-plusminus-a-value/#findComment-923891 Share on other sites More sharing options...
artacus Posted September 24, 2009 Share Posted September 24, 2009 PFM's approach will cause a full table scan. Which will be an issue if you've got a large table. Corbin's approach will perform better, but you don't necessarily need to preprocess in php. BETWEEN ($id - 5) AND ($id + 5) Quote Link to comment https://forums.phpfreaks.com/topic/175291-solved-plusminus-a-value/#findComment-923899 Share on other sites More sharing options...
_spaz Posted September 24, 2009 Author Share Posted September 24, 2009 Thanks guys this issue is now resolved with your help. Quote Link to comment https://forums.phpfreaks.com/topic/175291-solved-plusminus-a-value/#findComment-924467 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.