Reputation: 30815
I receive such an array:
[1,2,3,4,5];
And I need to implement a query that has many OR
s in WHERE
clause. I take values from my array. It looks like this:
SELECT foo, bar FROM tbl WHERE (a.bar = 1 OR a.bar = 2 OR a.bar = 3 ... and so on)
How may I create such a WHERE
part in node-mysql? Or how to pass it parameters?
Upvotes: 2
Views: 3517
Reputation: 12678
simple thing to do is to use the join()
of the array
var values = [1, 2, 3, 4, 5];
var query = 'SELECT foo, bar FROM tbl WHERE (a.bar = ';
query = query + values.join(' OR a.bar = ') + ')';
That will generate the query you're looking for
Upvotes: 1
Reputation: 41246
I know this is fairly old, but I've had success, at least with recent versions of node-mysql using this format:
var titles = ['title 1', 'title 2'];
var sql = 'SELECT t.id ' +
' FROM topics t ' +
' WHERE t.title IN (?)';
var params = [titles];
This translates pretty nicely to:
SELECT t.id FROM topics t WHERE t.title IN (\'title 1\', \'title 2\')
Much cleaner, I think.
Upvotes: 8