views:

75

answers:

4

I need to select some records from a table where a column value is equal to any of the values in a list.

This can be done using either IN operator (like WHERE column IN (v1, v2, ...)) or multiple OR operators (like WHERE column = v1 OR column = v2, ...).

When should I use either of these ways?

+3  A: 

There's no benefit, MySQL will transform an IN-Clause to these or-statements in parsing and optimizing this query;)
IN() is simply for shorter and more readable querys, use it when possible.

Tobias P.
+2  A: 

OR must be faster on small lists, as mysql isn't going to check further if it finds a match. when you use IN, mysql performs a binary search between the IN arguments. so, IN is going to be faster on big lists of values.

kgb
+1  A: 

As Tobias P. stated, the IN clause makes for a more readable query (thus, more maintainable code). This increased readability is probably most obvious when using a sub query:

WHERE column in (SELECT column FROM table WHERE x=4 AND y='some string' AND ...)

To answer your question directly, use the IN clause when it makes your query more readable.

labratmatt
A: 

Use IN. More readable plus better in performance. If there are too many ORs, anyway the optimizer is going to convert it into an IN query. In addition the IN query on the other hand is smaller (in size) and easier to parse compared to the OR query.

neal aise