Reputation: 187
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
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
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
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
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