# How to implement PostgreSQL's WHERE IN clause?

**URL:** <https://forum.crystal-lang.org/t/how-to-implement-postgresqls-where-in-clause/926>\
**Category:** Help & Support\
**Created:** [July 8, 2019, 2:07pm UTC](https://forum.crystal-lang.org/t/how-to-implement-postgresqls-where-in-clause/926 "2019-07-08T14:07:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![curious2learn](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@curious2learn](https://forum.crystal-lang.org/u/curious2learn)\
**Post date:** [July 8, 2019, 2:07pm UTC](https://forum.crystal-lang.org/t/how-to-implement-postgresqls-where-in-clause/926/1 "2019-07-08T14:07:13Z")

</div>

Problem:  
I have an array of `id` values; say, `idvals = ["id1", "id2", "id3"]`. I want to execute the following query:

```
select id, var1 from mytable
where 
id in ('id1', 'id2', 'id3');

```

How can I implement it in Crystal using `db` and `pg` libraries?

Thank you.

---

<div class="post-metadata">

**Author:** ![curious2learn](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@curious2learn](https://forum.crystal-lang.org/u/curious2learn)\
**Post date:** [July 8, 2019, 2:20pm UTC](https://forum.crystal-lang.org/t/how-to-implement-postgresqls-where-in-clause/926/2 "2019-07-08T14:20:23Z")

</div>

After posting, I searched for how this could be done in Golang. Found one possible solution. If you think there is a better way, please respond. Otherwise, this may be helpful to others.

```
idvals = ["id1", "id2", "id3"]
idvalsStr = "{" + idvals.join(", ") + "}"
conn.query_all("select id, var1 from mytable 
                          where id = Any($1)",
                          idvalsStr, 
                          as: {id: String, var1: String}
                       )
```

---

<div class="post-metadata">

**Author:** ![Fryguy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fryguy/32/1181_2.png) [@Fryguy](https://forum.crystal-lang.org/u/Fryguy)\
**Post date:** [July 8, 2019, 4:00pm UTC](https://forum.crystal-lang.org/t/how-to-implement-postgresqls-where-in-clause/926/3 "2019-07-08T16:00:14Z")

</div>

See the “Exec” section in [https://crystal-lang.org/reference/database/](https://crystal-lang.org/reference/database/) for how to use parameter substitution. I haven’t checked if Arrays can be passed via that mechanism, but that’s where I’d start.
