Harpo
Harpo

Reputation: 187

MySQL Full Text Search find NOT exact match

I can't find the answer to this on the internet, possibly because I don't know the right terms/jargon to search for.

I am using my own search function to get rows from my table. I have three rows with columns with this input (without quotes):

"食"
"食べる"
"食糧"

Now, when I search for "" I want the query to return all of these three results. But I only get the first one "", the exact match returned in my query. How can I grab all of them?

My query:

  $sw = '食';
  $query = 'SELECT *, MATCH(word) AGAINST("'.$sw.'") as score FROM wordlist WHERE MATCH(word) AGAINST("'.$sw.'") ORDER BY score DESC;';

I appreciate all help.

Edit: Adding % doesn't work, like "%'.$sw.'%". None of the answers so far work either. :/

Upvotes: 2

Views: 1551

Answers (4)

iubema
iubema

Reputation: 349

Maybe you should use LIKE comparison but if you want to stay using MATCH() AGAINST() try with this:

    $sw = '食';
  $query = 'SELECT *, MATCH(word) AGAINST("+'.$sw.'") as score FROM wordlist WHERE MATCH(word) AGAINST("+'.$sw.'") ORDER BY score DESC;';

Upvotes: 0

user3332631
user3332631

Reputation: 394

Try this:

$sw = '食';
$query = 'SELECT *, MATCH(word) AGAINST("'.$sw.'") as score FROM wordlist WHERE word LIKE "%'.$sw.'%" ORDER BY score DESC;';

In addition, you shuld check out escaping, sql injection and prepared statements.

Upvotes: 0

SagarPPanchal
SagarPPanchal

Reputation: 10111

Can you use this,

'SELECT *, MATCH(word) AGAINST("'.$sw.'") as score FROM wordlist WHERE MATCH(word) LIKE '%'.$sw.'%'  ORDER BY score DESC;'

instead of your own query

Upvotes: 0

Bart Friederichs
Bart Friederichs

Reputation: 33511

Use LIKE:

 $sw = '食';
 $query = '
         SELECT *, MATCH(word) AGAINST("'.$sw.'") as score 
         FROM wordlist 
         WHERE work LIKE "'.$sw.'%" 
         ORDER BY score DESC';

Upvotes: 1

Related Questions