Pass an array of integers in array of parameters
Another possibility is:
db.any("SELECT fieldname FROM table WHERE fieldname = $1 AND fieldname2 = any ($2)",
[1,[1730442,1695256,487413,454336]])
.then(function (data) {
console.log("DATA:", data); // print data;
})
.catch();
From postgresql manual here: https://www.postgresql.org/docs/9.5/static/functions-comparisons.html
The right-hand side is a parenthesized expression, which must yield an array value. The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ANY is "true" if any true result is obtained. The result is "false" if no true result is found (including the case where the array has zero elements).
If the array expression yields a null array, the result of ANY will be null. If the left-hand expression yields null, the result of ANY is ordinarily null (though a non-strict comparison operator could possibly yield a different result). Also, if the right-hand array contains any null elements and no true comparison result is obtained, the result of ANY will be null, not false (again, assuming a strict comparison operator). This is in accordance with SQL's normal rules for Boolean combinations of null values.
SOME is a synonym for ANY.
I am the author of pg-promise.
There is some confusion in your example...
You are using only two variables in the query, but passing in four values:
1
[[1730442],[1695256]]
[487413]
[454336]
And your syntax there isn't a valid JavaScript, as you are using ]
in the end without the matching opening one, so it is hard to understand what it is exactly you are trying to pass in.
And then why wrap all values in arrays again? I believe it is just a list of integers that you want inside the IN()
statement.
When you want to use values within WHERE IN()
, it is not really an array of those values that you want to pass in, it is a comma-separated list of values.
If you change your example to the following:
db.any('SELECT fieldname FROM table WHERE fieldname = $1 AND fieldname2 IN ($2:csv)',
[1, [1730442,1695256,487413,454336]])
You will get the correct list of values injected.
See also:
- CSV Filter
- WHERE col IN example.