question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proper syntax for "WHERE column IN [ArrayOfValues]"?

See original GitHub issue

I 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:closed
  • Created 12 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
whitelynxcommented, May 31, 2013

Since node-postgres already correctly renders JavaScript Arrays as PostgreSQL array literals, there’s a much simpler and less hackish solution possible; instead of using IN to test for membership, use = ANY(...):

client.query({
    text: "SELECT * FROM users"
            + " WHERE remote_id = ANY($1)"
            + " AND provider = $2",
    values: [
        [1, 3, 4], // remote_id values
        'Facebook', // provider
    ],
}, callback);

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.

0reactions
ringerccommented, Aug 19, 2015

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found