SQL IN 運(yùn)算符


 IN 運(yùn)算符允許您在 WHERE 子句中指定多個(gè)值。

 IN 運(yùn)算符是多個(gè) OR 條件的簡(jiǎn)寫。

SQL IN 語法

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);

演示數(shù)據(jù)庫


 在本教程中,我們將使用著名的 Northwind 示例數(shù)據(jù)庫。

 以下數(shù)據(jù)選取自"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 操作符實(shí)例


 以下 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 語句選取來自同一國家的所有客戶作為供應(yīng)商:

代碼示例:

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