MySQL match ( ) against ( )
Sorry I am such a novice at this. Just cannot get my head around match()
and against()
.
I have a set of 10 variables, essentially answers from 10 questions. I want to match them against an MYSQL table and then select 5 most relevant results in descending order.
I also want to display the percentage of the score for each of the result.
The variables are:
$a1 = y;
$a2 = n;
$a3 = n;
$a5 = y;
.
.
.
$a10 = n;
My table structure is something like this:
PERSON_NAME, AGE, A1, A2, A3, A5... A10
The results I want to be display is like this:
Peter Smith (100% match)
Jane Grey (65% match)
Jill Sanders (30% match)
Why does this not work?
Here is my code:
$query = "SELECT *, MATCH($a1,$a2,$a3, $a4,$a5,$a6,$a6,$a7,$a8,$a9,$a10) AGAINST('A1, A2, A3, A4, A5, A6, A7, A8, A9, A10') AS score FROM table_name WHERE MATCH($a1,$a2,$a3, $a4,$a5,$a6,$a6,$a7,$a8,$a9,$a10) AGAINST('A1, A2, A3, A4, A5, A6, A7, A8, A9, A10') ORDER BY SCORE DESC";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
---------
$max_score = 0;
$data = array();
do {
if ($row['score'] > $max_score) {
$max_score = $row['score'];
}
echo $row['PERSON_NAME']." ".@number_format(($row['score']/$max_score)*100,0)."%<br>n";
} while ($row = mysql_fetch_assoc($result));
You appear to be doing the search backwards. The syntax is
SELECT ... MATCH (field1, field2, ... , field N) AGAINST ('search string')
You're doing what appears to be the equivalent of
SELECT ... MATCH(y, n, y, y, ....) AGAINST ('field2 field2 ... fieldN')
Since you are HIGHLY unlikely to have fields named y
and n
in your table, you'll end up with a mysql syntax error due to the unknown/non-existent fields.
If you had even MINIMAL error handling in your script, such as
$result = mysql_query($sql) or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^
you'd have been told about the errors. NEVER assume success. Always assume failure, and treat success as a pleasant surprise.
链接地址: http://www.djcxy.com/p/75336.html上一篇: 选择所有具有特定号码的
下一篇: MySQL匹配()反对()