MySQL最近的匹配使用ABS混淆

Very Simple one, just stumped on syntax and not finding a good example anywhere.

I want to search for the closest match in a database. Earlier on the page I define $rating, and that is the value I want to match to. Uploaderating is the value I want to match to within the table.

I am using the following:

$SQL = "SELECT TOP(1) id
FROM table
WHERE uploader != '$username'
ORDER BY ABS(uploaderrating - $rating)";

$result = mysql_query($query);
$row1= mysql_fetch_array($result);
$id1 = $row1[id];

Instead of getting the row ID which is what I want, I get the following:

mysql_fetch_array(): supplied argument is not a valid MySQL result resource in mysite at row number.

Does the TOP function exist in your install my MySQL?

Try this syntax:

SELECT id
FROM test
WHERE uploader != '$username'
ORDER BY ABS(uploaderrating - $rating)
LIMIT 0,1

Demo can be found here.

If you're using mysql you have to use

SELECT id
...
limit 1