MySQL: Search for a sentence and ignore order of words

时间:2016-07-11 19:10:39

标签: php mysql sql

Is there something in MySQL will allow to search for sentence without care about the order of words, ex: Search for Europe league match league Europe, same with sentences contain 3 words ... etc

I know I can handle this in Programming language and generate MySQL query like this:

select p.title
from post p
where p.title = 'Europe league' or p.title = 'league Europe'
order by case
    when text = 'Europe league' then 1
    when text = 'league Europe' then 2
end
limit 10;

But is there a self handle in MySQL for this issue? thx

2 个答案:

答案 0 :(得分:4)

You're looking for full text search. Read up on the docs, so you can use the following kind of query:

SELECT p.title
FROM post p
WHERE MATCH (p.title) AGAINST ("+europe +league" IN BOOLEAN MODE)
LIMIT 10

答案 1 :(得分:-1)

Obviously the number of iterations becomes prohibitive as you add more words; one solution to this would be to use the instr function (http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_instr). That way you could do something along the lines of the following:

select p.title
from post p
where instr('Europe',p.title)>0
AND instr('League ',p.title)>0
AND instr('Match',p.title)>0
limit 10;