开发者

mysql most relevant search results for numbers

开发者 https://www.devze.com 2023-03-09 05:00 出处:网络
I am finding a lot of answers on most relevant searching for text results, but not much at all on numbers. What would be the best if there were a way for the MySQL query to return results in the most

I am finding a lot of answers on most relevant searching for text results, but not much at all on numbers. What would be the best if there were a way for the MySQL query to return results in the most relevant way for number searches. It would order by exact matches first and branch out the variable until all results were given.

EXAMPLE: Variable = 54

Results = 54,55,53,56,52,57,51...

Is there such a way to do this that anyone knows, and keep it all within a sin开发者_JS百科gle query? I imagine if I have to that I could put the query into a php loop and just increment the number that way, returning results as they were found. But that seems rather code intensive and would cause for some lag in the results.

I saw this thread, but I am not sure this answers my question.

Thanks in advance.


Perhaps ORDER BY ABS(column - 54)?

MySQL can't use an index for this, so it won't be particularly speedy, but it does what you want.


I think it would help if there was more information about your table. Are you talking about a number that's inside some text in a text or char field or are you talking about an actual numerical column?

If it's a numerical column why not just use a range? Like so:

SELECT number_field, ABS(number_field - $variable) as relevance 
 FROM table 
 WHERE number_field >= ($variable - 3) 
     AND number_field <= ($variable + 3)
 ORDER by relevance ASC, number_field DESC;

If $variable = 54 it should return the results you have above.

0

精彩评论

暂无评论...
验证码 换一张
取 消