適用範圍
在 WHERE 子句中指定多個值,可搭配 IN 來進行篩選
SELECT 欄位
FROM 資料表
WHERE 欄位IN (值, 值, ...);
SQL IN
在 WHERE 子句中指定多個值,可搭配 IN 來進行篩選。
IN (值,值,....) 子句在括弧內可以有一或多個值,而不同值之間由逗點分開。值可以是數值或是文字。若值的型態為文字時,需再IN 括號內中每個值需用'單引號'
在Select 子句中,想以City找尋有'Mexico'的值,則搭配WHERE 子句後使用 IN (值,值..)括號,
SELECT * FROM Customers WHERE City in('Mexico');
CustomerID |
CompanyName |
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 Constitucion 2222 |
Mexico |
05021 |
Mexico |
3 |
Antonio Moreno Taqueria |
Antonio Moreno |
Mataderos 2312 |
Mexico |
05023 |
Mexico |
4 |
Around the Horn |
Thomas Hardy |
120 Hanover Sq. |
London |
WA1 1DP |
UK |
5 |
Berglunds snabbkop |
Christina Berglund |
Berguvsvagen 8 |
Lulea |
S-958 22 |
Sweden |
6 |
Blauer See Delikatessen |
Hanna Moos |
Forsterstr. 57 |
Mannheim |
68306 |
Germany |
7 |
Blondesddsl pere et fils |
Frederique Citeaux |
24, place Kleber |
Strasbourg |
67000 |
France |
8 |
Bolido Comidas preparadas |
Martin Sommer |
C/ Araquil, 67 |
Madrid |
28023 |
Spain |
9 |
Bon app' |
Laurence Lebihan |
12, rue des Bouchers |
Marseille |
13008 |
France |
10 |
Bottom-Dollar Markets |
Elizabeth Lincoln |
23 Tsawassen Blvd. |
Tsawassen |
T2F 8M4 |
Canada |
11 |
Du monde entier |
Janine Labrune |
67, rue des Cinquante Otages |
Nantes |
44000 |
France |
結果:
CustomerID |
CompanyName |
ContactName |
Address |
City |
PostalCode |
Country |
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitucion 2222 |
Mexico |
05021 |
Mexico |
3 |
Antonio Moreno Taqueria |
Antonio Moreno |
Mataderos 2312 |
Mexico |
05023 |
Mexico |