Eddie D.
Eddie D.

Reputation: 145

use sql LIKE to match full words

i need to search for entire word not part of them

$q = $_GET['q'];
SELECT * FROM table WHERE column LIKE '% $q %'

for example if $q = mag it shouldn't find magazine i've tried solutions that users posted but it didn't work

like REGEX and CONCAT

ps: my db column is a big text

Upvotes: 1

Views: 115

Answers (3)

Gordon Linoff
Gordon Linoff

Reputation: 1271003

If your words are separated by spaces, then add spaces on both sides:

SELECT *
FROM table
WHERE concat(' ', column, ' ') LIKE '% $q %';

Upvotes: 0

M I
M I

Reputation: 3682

I dont know whether this will solve your issue or you have already tried but give it a try and look for word boundaries with the following REGEXP

SELECT * FROM table WHERE column  RLIKE "[[:<:]]$q[[:>:]]";

Upvotes: 1

Vikas Arora
Vikas Arora

Reputation: 1666

Don't use LIKE...Use only comparison in this case. Something like this:

SELECT * FROM table WHERE column = '$q';

Upvotes: 0

Related Questions