Proper syntax for "WHERE column IN [ArrayOfValues]"?
See original GitHub issueI have an array of user ids, and would like to select all the users at once.
This query:
this.db.query({
text: "SELECT * FROM users WHERE remote_id IN $1 AND provider = $2",
values: [array_of_ids, provider_string]
}, cb);
Gives the error error: syntax error at or near "$1"
What’s the correct syntax for this? Couldn’t find it in the documentation or via google.
Thanks.
Issue Analytics
- State:
- Created 12 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
SELECT WHERE column values are in array - sql
SELECT * FROM myTable WHERE columnB = ANY (ARRAY['Red', 'Blue', 'Green']);. Or with a literal array constant as input: SELECT * FROM myTable ......
Read more >Guidelines and examples of array formulas - Microsoft Support
An array formula is a formula that can perform multiple calculations on one or more items in an array. You can think of...
Read more >Array formulas and functions in Excel - examples and guidelines
The tutorial explains what is an array formula in Excel and provides examples of using Excel array functions and constants.
Read more >Working With Arrays | SQL Tutorial Documentation on data.world
The ARRAY command allows you to create an array from a group of values. ... The sole aggregation for arrays takes all the...
Read more >Using arrays in Google Sheets - Google Docs Editors Help
An array is a table (consisting of rows and columns) of values. ... you'll see its array result spill over to the cells...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Since node-postgres already correctly renders JavaScript
Array
s as PostgreSQL array literals, there’s a much simpler and less hackish solution possible; instead of usingIN
to test for membership, use= ANY(...)
:There is a full example in this gist: https://gist.github.com/whitelynx/5686162
I’m not sure what the performance considerations are of doing this instead of doing an actual
IN
clause; I haven’t done any benchmarks on it. I wouldn’t expect it to perform too differently, though.It might be a good idea to add this to the answers for the FAQ question on this topic.
@whitelynx I’ve updated the FAQ entry for this to reflect what you’ve said above. As I do not actually use node-postgres myself (I’m a PostgreSQL dev / Stack Overflow helper / PgJDBC contributor) I’d appreciate it if you could sanity-check the change and, if possible, add links to any node-postgres documentation that might exist to explain array parameters.