Is there a better way to create a WHERE IN clause?
See original GitHub issueI 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:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top 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 >
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 Free
Top 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

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
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.skipAnalysisfunction when you don’t care 😉