# MySQL IN clause for an array of strings

**URL:** <https://www.ruby-forum.com/t/mysql-in-clause-for-an-array-of-strings/131984>\
**Category:** Rails\
**Created:** [March 14, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/mysql-in-clause-for-an-array-of-strings/131984 "2008-03-14T21:21:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [March 14, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/mysql-in-clause-for-an-array-of-strings/131984/1 "2008-03-14T21:21:26Z")

</div>

I’m trying (for the first time) to build an IN clause from an array of  
strings and getting an error using MySQL as the DB…

Here’s the code I’m using to build the clause:

```
zips_clause = ''
user.get_zipcodes.each {|z|
  zips_clause += ',' if !zips_clause.blank?
  zips_clause += '\'' + z + '\'' # wrap string in single-quotes
}
zips_clause = '(' + zips_clause + ')'

conditions = sanitize_sql(["updated_at > ? AND local_code IN ?",

```

since\_date, zips\_clause])

This is the resulting SQL:

SELECT \* FROM tips WHERE (updated\_at \> ‘2008-03-14 11:55:29’ AND  
local\_code IN ‘(‘94952’,‘08054’,‘11111’)’)

And this is the error:

Mysql::Error: You have an error in your SQL syntax; check the manual  
that corresponds to your MySQL server version for the right syntax to  
use near ‘’(‘94952’,‘08054’,‘11111’)’)’ at line 1: SELECT \* FROM  
tips WHERE (updated\_at \> ‘2008-03-14 11:55:29’ AND local\_code IN  
‘(‘94952’,‘08054’,‘11111’)’)

I’m guessing that it’s something about that parenthesized clause that  
follows the IN.

Could it be the backslash that gets included for the single-quotes  
around each string? And why _is_ that getting inserted?

Or can anybody spot anything else wrong with that SQL statement?

If you have code that shows a different way to build a SQL IN clause  
from an array of strings that might help, too.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [March 14, 2008, 9:23pm UTC](https://www.ruby-forum.com/t/mysql-in-clause-for-an-array-of-strings/131984/2 "2008-03-14T21:23:12Z")

</div>

> ```
> zips_clause = '(' + zips_clause + ')'
> 
> conditions = sanitize_sql(["updated_at > ? AND local_code IN ?",
> 
> ```
> 
> since\_date, zips\_clause])

a = [1,2,3,4]  
User.find(:all, :conditions =\> [“id IN (?)”, a])

Results in this:

SELECT \* FROM users WHERE (id IN (1,2,3,4))

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [March 14, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/mysql-in-clause-for-an-array-of-strings/131984/3 "2008-03-14T22:11:06Z")

</div>

Thanks, Phillip!

I had tried something very close to what you show me, but it didnt  
work… I was missing the parentheses around the ? in ‘(?)’.

Sometimes any docs for this are so hard to find (I’d tried googling,  
the Agile book, Rails Way, etc.)

Thanks again.
