MYSQL fulltext search order by relevance MYSQL fulltext search order by relevance php php

MYSQL fulltext search order by relevance


From MySQL Boolean Full-Text Searches documentation:

They do not automatically sort rows in order of decreasing relevance. You can see this from the preceding query result: The row with the highest relevance is the one that contains “MySQL” twice, but it is listed last, not first.

That explains why it is not sorted by relevance without the ORDER BY. Now to be able to order by relevance, you need to define it:

SELECT *, MATCH (`Badge`,`First Name`,`Last Name`,`Service Tag`,`Asset Tag`) as relevanceWHERE MATCH AGAINST ('".$query."*' IN BOOLEAN MODE) and `deleted` = '0'ORDER BY relevance DESC