sqlmysqlnode.js

IN clause in mysql nodejs


I have a simple Node.js application that executes the following query:

select * from User where userid in (?)

The userids I get are from a JSON array sent from the client side. How can I use that in this select query? I tried:

  1. As itself, but it's not working.
  2. Convert this to a Javascript array, also not working.

Solution

  • If you are using node module like mysql this can be accomplished with the following:

    const query = "SELECT * FROM User WHERE userid IN (?);";
    const data= ["a", "b", "c"];
    
    conn.query(query, [data], function (err, results) {});
    

    According to the documentation, "Arrays are turned into a list, e.g. \['a', 'b'\] turns into 'a', 'b'".