Home > Database > Mysql Tutorial > body text

How can I sort MySQL fulltext search results by relevancy?

DDD
Release: 2024-11-03 02:19:29
Original
969 people have browsed it

How can I sort MySQL fulltext search results by relevancy?

Sorting MySQL Fulltext Search Results by Relevancy

When working with MySQL, it's essential to understand the difference between LIKE and fulltext search for accurate result sorting.

In the provided query:

$query="SELECT * from `vocabulary` WHERE translation = 'word' OR translation LIKE '%word%'";
Copy after login

The LIKE operator is used for partial matching, not fulltext search. To perform a fulltext search, use the MATCH(...) AGAINST(...) syntax.

For example:

$query="SELECT * from `vocabulary` WHERE MATCH(translation) AGAINST('+word')";
Copy after login

The MATCH function returns a matching score that can be used as an approximation of relevancy. This score can be used for sorting the results. For example:

$query="SELECT *, MATCH(translation) AGAINST('+word') AS score FROM `vocabulary` ORDER BY score DESC";
Copy after login

This query sorts the results by relevancy, with the highest-scoring matches appearing first.

The above is the detailed content of How can I sort MySQL fulltext search results by relevancy?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template