0

I am trying to do a wildcard search on one of the DB columns. The input params are passed as an array of string. Example:

{ name: ['John','Doe'] }

Need to return (id: 1, id:2) as first element matches id:1 and second element matches id: 2. How to effectively use like statement.

|  name (string)         | id | 
|------------------------|----|
| John,steve             |  1 |   
| clive,steve,Doe        |  2 |    
| suzan                  |  3 |   

1 Answer 1

1

Looks like your best option will be to use the regexp operator in the sql query.

The raw SQL would look something like:

SELECT id FROM table_name WHERE name REGEXP 'John|Doe';

In Sequelize, your model query might looks something like:

someModel.findAll({
  where: {
    name: {
      [Op.regexp]: 'John|Doe',
    },
  },
  attributes: ['id'],
})

I imagine that the list of names to check for would be dynamic so just replace the value assigned to the regex operator above to a string you've built.

const names = ['John', 'Doe'];
const nameRegexStr = names.join('|');
Sign up to request clarification or add additional context in comments.

Comments

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.