4

I want to check if a column in my database contains words similar to my sample string. The opposite of

select * from myTable where name like '%words%';

so that if I have record with name=word I could retrieve it. With sample above I can only get result where words is a sub string of name column in myTable, therefore I cant get word

1 Answer 1

4

You just flip the two terms in your LIKE operator:

SELECT * 
FROM mytable
WHERE 'words' LIKE CONCAT('%',name,'%')

I believe that LOCATE() and INSTR() may work here too which looks nicer since there isn't a need for concatenating the search term/substring.

SELECT *
FROM mytable
WHERE INSTR('words', name) > 0
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. How is this CONCAT('%',name,'%') working? Is there a link you could forward me to?
It's merely tossing the wild cards around each name and searching that. It's exactly the same as your example but instead of saying Where this "name" column is like this string we are saying Where this string is like this name column. Not sure if there is documentation since it's just the LIKE operator doing its thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.