Currently, in node.js, if I want to search for a string in multiple columns in a MySQL table I have to do like this:
mysqlConnection.query(
"SELECT * FROM my_table WHERE column_a LIKE ? OR column_b LIKE ? OR column_c LIKE ?",
[searchString, searchString, searchString],
callback
);
I want to be able to do it like this:
mysqlConnection.query(
"SELECT * FROM my_table WHERE column_a LIKE ? OR column_b LIKE ? OR column_c LIKE ?",
searchString, callback
);
Or maybe even with named parameters:
mysqlConnection.query(
"SELECT * FROM my_table WHERE column_a LIKE :searchString OR column_b LIKE :searchString OR column_c LIKE :searchString",
{searchString: "needle"},
callback
);
Is there a way I can do it?
you can enable named parameter when creating your db pool connection.
const connection = mysql.createPool({
...yourConfig,
namedPlaceholders: true,
});
edit: As @robertklep explained it only works on mysql2 npm package
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With