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?

Nathan Xabedi
  • 1,097
  • 1
  • 11
  • 18
  • you should use [IN](https://www.w3resource.com/mysql/comparision-functions-and-operators/in-function.php) operator and look into this [answer](https://stackoverflow.com/questions/1127088/mysql-like-in) – Vishnu Bhadoriya Aug 20 '21 at 06:10

1 Answers1

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);
kol
  • 27,881
  • 12
  • 83
  • 120