1

I have array of string like this

let search = ["user","country"];

I want to get data from mysql database using LIKE operator.

For example like this

searchStmnt = `u.u_fullname LIKE "%` + search + `%"`

But the above code is not working. Can anyone suggest me how to do that?

1
  • you should use IN operator and look into this answer Commented Aug 20, 2021 at 6:10

1 Answer 1

2

const search = ["user", "country"];
const searchStmnt = search.map(item => `u.u_fullname LIKE '%${item}%'`).join(" OR ");
console.log(searchStmnt);

...or with REGEXP:

const search = ["user", "country"];
const searchStmnt = `u.u_fullname REGEXP '(${search.join("|")})'`;
console.log(searchStmnt);

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.