English 中文(简体)
mysql最相关的数字搜索结果
原标题:mysql most relevant search results for numbers
  • 时间:2011-05-29 03:55:00
  •  标签:
  • php
  • mysql

我在最相关的文本搜索结果中找到了很多答案,但在数字方面却不多。如果MySQL查询能够以与数字搜索最相关的方式返回结果,那么最好的方法是什么。它会先按精确匹配排序,然后分支变量,直到给出所有结果。

EXAMPLE: Variable = 54

结果=54,55,53,56,52,57,51。。。

有没有这样一种方法可以做到这一点,让任何人都知道,并将其全部保存在一个查询中?我想,如果必须这样做,我可以将查询放入php循环中,并以这种方式递增数字,在找到结果时返回结果。但这似乎是代码密集型的,会导致结果出现一些滞后。

我看到了线程,但我不确定这是否能回答我的问题。

提前谢谢。

最佳回答

也许ORDER BY ABS(第54列)

MySQL不能使用索引,所以速度不会特别快,但它可以随心所欲。

问题回答

我想如果有更多关于你桌子的信息会有所帮助。你说的是文本或字符字段中某个文本中的一个数字,还是一个实际的数字列?

如果它是一个数字列,为什么不使用一个范围呢?就像这样:

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;

如果$variable=54,它应该返回上面的结果。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签