Saturday, 8 April 2017

node-mysql single value for multiple query parameters

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?



via starleaf1

No comments:

Post a Comment