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.

Is there a better way to create a WHERE IN clause?

See original GitHub issue

I need to create a query which uses WHERE IN, like:

SELECT * FROM X WHERE X.ID IN (a, b, c, d, ...)

Right now I am manually putting together the query as a string, which feels … not optimal.

let set (blobIds: BlobId list) =
    let whereIn =
        System.String.Join(", ", blobIds |> List.map (fun blobId -> sprintf "'%O'" blobId.Value))
        
    connect()
    |> Sql.query (sprintf "SELECT b.* FROM blob b WHERE b.id IN (%s)" whereIn)
    |> Sql.executeAsync map
    |> Sql.throwOrUnwrap

Is there a built-in way that I missed?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Zaid-Ajajcommented, Oct 13, 2020

Hi there,

Postgres has a built-in called ANY for these types of queries:

SELECT * FROM blob WHERE blob.id = ANY(@blobIds)

Use the function Sql.intArray to provide the integers of the @blobIds input parameter

1reaction
Zaid-Ajajcommented, Dec 14, 2020

The only problem you might come across is that the analyzer doesn’t understand new functions that you add yourself and might think you are missing parameters. I don’t know if that is the case, but consider opening issues op those if they popup or just use Sql.skipAnalysis function when you don’t care 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Any way to faster sql query containing IN clause where ...
I think right or left join is the better alternative. · Your query is fine.
Read more >
How to Write a WHERE Clause in SQL
Another way to use the IN operator is with a subquery to generate a list from another table. To understand this better, imagine...
Read more >
Tips on Constructing an Efficient WHERE Clause
Similarly WHERE clauses that refer to the left-most two columns can be even more efficient. Queries can achieve maximum efficiency when they use...
Read more >
SQL WHERE IN - Explained With Examples
If you want to filter results based on a list of possible values, you can use the SQL WHERE IN clause. This clause...
Read more >
Combine the functionality of IN and LIKE in a WHERE clause
You can't do an IN and LIKE with that Syntax. If you could, it would not really be better than the OR solution,...
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