简体   繁体   English

MySQL按Where子句匹配的顺序排序结果

[英]MySQL Ordering Results by the order of Where Clause Matched

I have a query that tokenizes a phrase on spaces and needs to look for the best match for the phrase. 我有一个查询,该查询在空间上标记了一个短语,并且需要寻找该短语的最佳匹配。 It needs to return that match first. 它需要先返回该匹配项。 I'm trying to do this in a single query using where clauses. 我正在尝试使用where子句在单个查询中执行此操作。 Is this possible? 这可能吗? I'm trying something along these lines: 我正在尝试以下方法:

for: my phrase 为:我的短语

select name from myTable where name="my phrase" or name like "%my phrase%" or (name like "%my%" and name like "%phrase%") or (name like "%my%" or name like "%phrase%") limit 5; 从myTable中选择名称,其中name =“ my短语”或“%my短语%”之类的名称或(%%%%之类的名称,“%phrase%”之类的名称)或(%%%的名称之类的名称或“%phrase%”)限制5;

Basically I want the top five matches in order of where clause matched. 基本上,我想按where子句的顺序匹配前5个匹配项。 I know that by default MySQL evaluates the where clauses in an optimized manner. 我知道默认情况下,MySQL以优化的方式评估where子句。 Is there any way to force it to evaluate them in order? 有什么方法可以迫使它按顺序评估它们? And if so, how can I make it return the results in order of where clause matched? 如果是这样,如何使它按where子句匹配的顺序返回结果?

Use Fulltext search instead of this. 请使用全文搜索。 It's much faster, more flexible and let you score results. 它更快,更灵活,可让您对结果进行评分。 Google for more Google更多

SELECT ..., MATCH(col_name) AGAINST ('my pharse') AS score FROM tbl_name WHERE MATCH(col_name) AGAINST('my pharse') ORDER BY score DESC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM