Skip to content
Advertisement

Using a WHERE ___ IN ___ statement

I’m trying to figure out how to properly use a WHERE _ IN _ statement

Definition:

I’m trying to do something like this:

Alternatively, I’ve also tried this, which directly evaluates to the above

The error I am getting is:

This is giving me an error. When I do it this way, it works, but this is not recommended as it is vulnerable to a SQL injection attack.

Advertisement

Answer

You need to create enough parameters to match your list of vars:

Note that you pass in list_of_vars as the parameter values list. Using the ', '.join() we generate a string of ? characters separated by commas, then use .format() to insert that into the statement.

For a long list of variables, it may be more efficient to use a temporary table to hold those values, then use a JOIN against the temporary table rather than an IN clause with bind parameters.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement