If you want the relevance and also the results to be sorted by relevance the common format of a FULLTEXT query is:
SELECT name, MATCH(name) AGAINST('Bob') AS relevance FROM users WHERE MATCH(name) AGAINST('Bob')
As a developer I always like to make my code DRY(don't repeat yourself). Is there any reason not to write the query as:
SELECT name, MATCH(name) AGAINST('Bob') AS relevance FROM users HAVING relevance > 0 ORDER BY relevance DESC
It seems to return the same results but should I be worried about the ORDER BY causing the query to be slower? Are these queries equivalent?
Specifying MATCH()
twice does not decrease performance as noted in the MySQL manual.
Natural Language Full-Text Searches
To achieve this result, you should specify
MATCH()
twice: once in theSELECT
list and once in theWHERE
clause. This causes no additional overhead, because the MySQL optimizer notices that the twoMATCH()
calls are identical and invokes the full-text search code only once.