9

我试图让我的全文搜索按相关性排序。如果删除 ORDER BY 但不按相关性排序,这是我的代码。我已经尝试过了,它实际上做到了,所以它根本找不到任何结果......有什么想法吗?

$query_for_result=mysql_query("
     SELECT * FROM Assets WHERE MATCH 
    (`Badge`,`First Name`,`Last Name`,`Service Tag`,`Asset Tag`)
     AGAINST ('".$query."*' IN BOOLEAN MODE) and
   `deleted` = '0'  ORDER BY relevance DESC");

编辑*

<input type="text" name="query" />
<input type="hidden" value="Search" name="submit" />
<input type="submit" name="submit" value="Search" />

</form>
<h4>Search by: Badge, First or Last Name, Service Tag, Asset Tag, Printer Queue or Printer IP.</h4>
<br>
</center>

<?php


if(isset($_GET['submit'])){


$db_tb_name=Assets;
$db_tb_atr_name=`First Name`;

$query=mysql_real_escape_string($_GET['query']);

$query_for_result=mysql_query("
 SELECT * FROM Assets WHERE MATCH 
(`Badge`,`First Name`,`Last Name`,`Service Tag`,`Asset Tag`)
 AGAINST ('".$query."*' IN BOOLEAN MODE) and
`deleted` = '0'  ORDER BY relevance DESC");

然后在代码中

while($data_fetch=mysql_fetch_array($query_for_result))
{

print '<h3>';   
print "<a href=\"modify.php?id=" . $data_fetch['id'] . "\">" . $data_fetch['First Name']  . ' ' . $data_fetch['Last Name'] . "</a>";
print '<br/><b>Badge:</b> '. $data_fetch['Badge'];
print '<br/> <b>Service Tag:</b> '. $data_fetch['Service Tag'];
print ' <b>Asset Tag:</b> '. $data_fetch['Asset Tag'];
print '<br/> <b>Status:</b> '. $data_fetch['Status'];
print '<br/><b>Employee Status: </b>';
    if( $data_fetch['Employee Status'] == 'Active' ){
print '<font color="#32CD32">' . $data_fetch['Employee Status'] . '</font>';
    }
elseif( $data_fetch['Employee Status'] == 'Terminated' ){
print '<font color="red">' . $data_fetch['Employee Status'] . '</font>';}
print '<br/> <b>Last Modified:</b> '. $data_fetch['Last Modified'];
print "<span> </span>";
print '</h3>';
print '<br/><p>' ; 

}

更新 这对我有用,我终于通过使用它来工作了

$query_for_result=mysql_query("SELECT  *, MATCH 
(`Badge`,`First Name`,`Last     Name`,`Service Tag`,`Asset Tag`) 
AGAINST ('".$query."'IN BOOLEAN MODE) AS Relevance FROM Assets WHERE 1 AND MATCH 
(`Badge`,`First Name`,`Last Name`,`Service Tag`,`Asset Tag`)
AGAINST ('".$query."' IN BOOLEAN MODE) and
 `deleted` = '0' ORDER BY Relevance DESC "); 
4

1 回答 1

8

来自MySQL 布尔全文搜索文档

它们不会自动按照相关性递减的顺序对行进行排序。从前面的查询结果可以看出:相关性最高的行是两次包含“MySQL”的行,但它是最后列出的,而不是第一个。

这就解释了为什么没有ORDER BY. 现在为了能够通过 订购relevance,您需要定义它:

SELECT *, MATCH (`Badge`,`First Name`,`Last Name`,`Service Tag`,`Asset Tag`) as relevance
WHERE MATCH AGAINST ('".$query."*' IN BOOLEAN MODE) and `deleted` = '0'
ORDER BY relevance DESC
于 2012-11-21T13:41:30.523 回答