我有一个包含元记录的表。我需要记录中的最佳搜索结果。获取MYSQL最佳搜索结果
Suppos如果我在表中搜索与以下关键词:
Beautiful well-maintained Remodeled
然后我的结果应该是这样的类型:
首先所有记录包含所有三个关键词, 二记录包含任何两个记录 和最后记录包含任何一个关键字。
我尝试过全文搜索和Like查询,但没有得到结果。
我有一个包含元记录的表。我需要记录中的最佳搜索结果。获取MYSQL最佳搜索结果
Suppos如果我在表中搜索与以下关键词:
Beautiful well-maintained Remodeled
然后我的结果应该是这样的类型:
首先所有记录包含所有三个关键词, 二记录包含任何两个记录 和最后记录包含任何一个关键字。
我尝试过全文搜索和Like查询,但没有得到结果。
这个计数每记录匹配OND顺序:
select *
from
(
select
mytable.*,
case when lower(col) like '%beautiful%' then 1 else 0 end +
case when lower(col) like '%well-maintained%' then 1 else 0 end +
case when lower(col) like '%remodeled%' then 1 else 0 end as matches
from mytable
)
where matches > 0
order by matches desc;
我会用PHP explode获得分隔每个单词,并使用喜欢调整自己的SQL查询相应使用AND和OR语句:
$keywords = explode(" ", $_REQUEST['q']);
$sql_query = $dbh->prepare("QUERY");
$i = 0;
foreach($keywords as $keyword) {
$sql_query->bindParam($i, $keyword);
}
我知道这个代码单独不会工作,但它应该指向你在正确的方向。
使用像
有三:
SELECT * FROM <table>
WHERE <column> LIKE '%Beautiful%'
AND <column> LIKE '%well-maintained%'
AND <column> LIKE '%Remodeled%'
任意两个:
SELECT * FROM <table>
WHERE (<column> LIKE '%Beautiful%' AND <column> LIKE '%well-maintained%')
OR (<column> LIKE '%Remodeled%' AND <column> LIKE '%well-maintained%')
OR (<column> LIKE '%Beautiful%' AND <column> LIKE '%Remodeled%')
任何一个:
SELECT * FROM <table>
WHERE <column> LIKE '%Beautiful%'
OR <column> LIKE '%Remodeled%'
OR <column> LIKE '%Beautiful%'
使用正则表达式任何比赛:
SELECT * FROM <table> WHERE <column> REGEXP '(.*)Remodeled(.*)|(.*)well-maintained(.*)|(.*)Beautiful(.*)'
你能告诉我们您的查询? – 2014-10-09 13:31:28
http://stackoverflow.com/questions/26279574/if-search-string-full-match-then-record-should-come-first-otherwise-it-should-be – JNevill 2014-10-09 13:34:34