Shubham Mundhra
Shubham Mundhra

Reputation: 21

In Postgres search for multiple words which are partially present in a string

I'm implementing a rudimentary form of the search. I would like to find all products names that contain all words partially in a search query.

So if I have these two products:

I want each individual word in the list of words to be partially present in the string. If any word is not present partially I should discard that row.

the search query: cucumb deod should match only Deoderant with a cucumber flavor.

Upvotes: 1

Views: 532

Answers (1)

Tov Aqulic
Tov Aqulic

Reputation: 325

You have to split the words in your search string and combine them in your query so that in result it looks like this:

... where name like '%cucumb%' and name like '%deod%' ...

Of course, in your code you would create a parameterized query, e.g.:

... where name like ? and name like ? ...

and set the parameters' values accordingly ('%cucumb%' and '%deod%' in the example), depending on the language / API / framework used.

Upvotes: 1

Related Questions