轻松上手,快乐学习!

SQL IN 运算符


SQL IN 运算符

IN运算符允许您在WHERE子句中指定多个值。

IN运算符是多个OR条件的简写。

在语法中

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

或者:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

演示数据库

以下是Northwind示例数据库中“Customers”表的选择:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constituciуn 2222 Mйxico D.F. 05021 Mexico
3 Antonio Moreno Taquerнa Antonio Moreno Mataderos 2312 Mйxico D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbkцp Christina Berglund Berguvsvдgen 8 Luleе S-958 22 Sweden


IN 运算符实例

以下SQL语句选择位于“Germany”,“France”和“UK”的所有客户:

实例

SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');

运行实例»

以下SQL语句选择不在“Germany”,“France”和“UK”中的所有客户:

实例

SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');

运行实例»

以下SQL语句选择与供应商来自同一Country 的所有客户:

实例

SELECT * FROM Customers
WHERE Country IN (SELECT Country FROM Suppliers);

运行实例»