This tutorial explains how to use the MySQL IN condition with syntax and examples.
The MySQL IN condition is used to help reduce the need to use multiple OR conditions in a SELECT, INSERT, UPDATE, or DELETE statement.
The syntax for the IN condition in MySQL is:
OR
The value to test.
These are the values to test against expression. If any of these values matches expression, then the IN condition will evaluate to true. This is a quick method to test if any one of the values matches expression.
This is a SELECT statement whose result set will be tested against expression. If any of these values matches expression, then the IN condition will evaluate to true.
Let's look at a MySQL IN condition example using character values.
The following is a MySQL SELECT statement that uses the IN condition to compare character values:
This MySQL IN condition example would return all rows from the contacts table where the last_name is either Johnson, Mark or Smith. Because the * is used in the SELECT, all fields from the contacts table would appear in the result set.
The above IN example is equivalent to the following SELECT statement:
As you can see, using the MySQL IN condition makes the statement easier to read and more efficient.
Next, let's look at a MySQL IN condition example using numeric values.
For example:
This MySQL IN condition example would return all suppliers where the supplier_id is either 200, 201, 203, or 300.
The above IN example is equivalent to the following SELECT statement:
Finally, let's look at an IN condition example using the NOT operator.
For example:
This MySQL IN condition example would return all rows from the contacts table where the last_name is not Johnson, Mark, or Smith. Sometimes, it is more efficient to list the values that you do not want, as opposed to the values that you do want.