A2-02-07.DML-Querying Data with MySQL IN Operator

转载自:http://www.mysqltutorial.org/sql-in.aspx

 

Querying Data with MySQL IN Operator

 

Summary: in this tutorial, you will learn how to use MySQL IN operator to determine if a specified value matches any value in a list or a subquery.

Introduction to the MySQL IN Operator

The IN  operator allows you to determine if a specified value matches any one of a list or a subquery. The following illustrates the syntax of the IN  operator.

Let’s examine the query in more detail:

  • You can use a column or an expression ( expr ) with the IN operator in the WHERE clause.
  • The values in the list must be separated by a comma (,).
  • The IN operator can also be used in the WHERE clause of other statements such as INSERTUPDATEDELETE, etc.

The IN operator returns 1 if the value of the column_1 or the result of the expr  expression is equal to any value in the list, otherwise, it returns 0.

When the values in the list are all constants:

  • First, MySQL evaluates the values based on the type of the column_1 or result of the  expr  expression.
  • Second, MySQL sorts the values.
  • Third, MySQL searches for values using binary search algorithm. Therefore, a query that uses the IN  operator with a list of constants will perform very fast.

Note that if the expr or any value in the list is NULL, the IN  operator returns NULL

You can combine the IN operator with the NOT operator to determine if a value does not match any value in a list or a subquery.

MySQL IN examples

Let’s practice with some examples of using the IN  operator.

Offices Table

If you want to find out the offices that locate in the U.S. and France, you can use the IN operator as the following query:

Try It Out

MySQL IN country list example
You can achieve the same result with the OR operator as the following query:

Try It Out

In case the list has many values, you have to construct a very long statement with multiple OR operators. Hence, the IN operator allows you to shorten the query and make the query more readable.

To get offices that do not locate in USA and France, you use NOT IN  in the WHERE clause as follows:

Try It Out

MySQL NOT IN offices example

MySQL IN with subquery

The IN operator is often used with a subquery. Instead of providing a list of constant values, the subquery provides the list of values.

Let’s take a look at the orders and orderDetails tables:

order-orderDetails-tables

For example, if you want to find orderwhose total amounts are greater than 60000, you use the INoperator as the following  query:

Try It Out

MySQL IN with Subquery example

The whole query above can be broken down into 2 queries.

First, the subquery returns a list of order numbers that have total greater than 60000 using the GROUP BY and HAVING clauses on the orderDetails table.

Try It Out

MySQL IN subquery

Second, the main query gets the data from the orders table and applies the IN operator in the WHEREclause.

Try It Out

In this tutorial, we have shown you how to use MySQL IN operator to determine if a value matches any value in a list or a subquery.

posted @ 2018-08-21 15:41  zhuntidaoren  阅读(140)  评论(0编辑  收藏  举报