目录
SQL DELETE 语句
DELETE 语句
演示数据库
DELETE 实例
删除所有行
SQL TOP, LIMIT, ROWNUM 子句
TOP 子句
演示数据库
SQL TOP、LIMIT 和 ROWNUM 示例
SQL TOP PERCENT 实例
添加WHERE子句
SQL MIN() 和 MAX() 函数
MIN() 和 MAX() 函数
演示数据库
MIN() 实例
MAX() 实例
SQL COUNT(), AVG() 和 SUM() 函数
COUNT(), AVG() 和 SUM() 函数
演示数据库
COUNT() 实例
AVG() 实例
演示数据库
SUM() 实例
SQL LIKE 操作符
SQL LIKE 操作符
演示数据库
LIKE 操作符实例
SQL 通配符
SQL 通配符
演示数据库
使用 % 通配符
使用 _ 通配符
使用 [charlist] 通配符
使用 [!charlist] 通配符
SQL IN 操作符
SQL IN 操作符
演示数据库
IN 操作符实例
SQL BETWEEN 操作符
SQL BETWEEN 操作符
演示数据库
BETWEEN 实例
NOT BETWEEN 实例
带有 IN 的 BETWEEN 操作符实例
带有文本值的 BETWEEN 操作符实例
带有文本值的 NOT BETWEEN 操作符实例
示例表
带有日期值的 BETWEEN 操作符实例
SQL 别名
SQL 别名
演示数据库
Alias 实例: 使用一个列名别名
Alias 实例: 使用表名称别名
SQL 联接
SQL JOIN
不同类型的 SQL JOIN
DELETE 语句用于删除表中的记录。
【DELETE 语法】
DELETE FROM table_name WHERE condition;
注: 请注意 SQL DELETE 语句中的 WHERE 子句!WHERE 子句规定哪条记录或者哪些记录需要删除。如果您省略了 WHERE 子句,所有的记录都将被删除!
以下是从示例数据库的 "客户(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 |
以下 SQL 语句从 客户(customer) 表中删除客户 "Alfreds Futterkiste":
【实例】
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
客户(Customers) 表现在如下所示:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
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 |
可以在不删除表的情况下删除所有的行。这意味着表的结构、属性和索引都是完整的:
DELETE FROM table_name;
以下 SQL 语句删除 "Customers" 表中的所有行,而不删除该表:
【实例】
DELETE FROM Customers;
TOP 子句用于规定要返回的记录的数目。
TOP 子句对于包含数千条记录的大型表很有用。返回大量记录可能会影响性能。
注: 并非所有数据库系统都支持 SELECT TOP子句。MySQL 使用 LIMIT,而 Oracle 使用 ROWNUM。
【SQL Server / MS Access 的语法:】
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
【MySQL 语法:】
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
【Oracle 语法:】
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
以下是从示例数据库的 "客户(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 |
以下 SQL 语句从 "Customers" 表中选择前三条记录(用于 SQL Server/MS Access):
【实例】
SELECT TOP 3 * FROM Customers;
下面的 SQL 语句显示使用 LIMIT 子句的等效示例(用于 MySQL):
【实例】
SELECT * FROM Customers
LIMIT 3;
下面的 SQL 语句显示使用 ROWNUM 子句的等效示例(用于 Oracle):
【实例】
SELECT * FROM Customers
WHERE ROWNUM <= 3;
在 Microsoft SQL Server 中还可以使用百分比作为参数。
以下 SQL 语句从 "Customers" 表中选择前 50% 的记录(用于SQL Server/MS Access):
【实例】
SELECT TOP 50 PERCENT * FROM Customers;
以下 SQL 语句从 "Customers" 表中选择前三条记录,其中 country 为 "Germany"(用于SQL Server/MS Access):
【实例】
SELECT TOP 3 * FROM Customers
WHERE Country='Germany';
下面的 SQL 语句显示了使用 LIMIT 子句的等效示例(用于 MySQL):
【实例】
SELECT * FROM Customers
WHERE Country='Germany'
LIMIT 3;
下面的 SQL 语句显示了使用 ROWNUM 子句的等效示例(用于 Oracle):
【实例】
SELECT * FROM Customers
WHERE Country='Germany' AND ROWNUM <= 3;
MIN 函数返回一列中的最小值。NULL 值不包括在计算中。
MAX 函数返回一列中的最大值。NULL 值不包括在计算中。
【MIN() 语法】
SELECT MIN(column_name)
FROM table_name
WHERE condition;
【MAX() 语法】
SELECT MAX(column_name)
FROM table_name
WHERE condition;
以下是从示例数据库的 "Products" 表中选择的内容:
ProductID | ProductName | SupplierID | CategoryID | Unit | Price |
---|---|---|---|---|---|
1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18 |
2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19 |
3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10 |
4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22 |
5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35 |
以下 SQL 语句查找最便宜产品的价格:
【实例】
SELECT MIN(Price) AS SmallestPrice
FROM Products;
以下 SQL 语句查找最昂贵产品的价格:
【实例】
SELECT MAX(Price) AS LargestPrice
FROM Products;
COUNT() 函数返回匹配指定条件的行数。
AVG 函数返回数值列的平均值。NULL 值不包括在计算中。
SUM 函数返回数值列的总数(总额)。
【COUNT() 语法】
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
【AVG() 语法】
SELECT AVG(column_name)
FROM table_name
WHERE condition;
【SUM() 语法】
SELECT SUM(column_name)
FROM table_name
WHERE condition;
以下是从示例数据库的 "Products" 表中选择的内容:
ProductID | ProductName | SupplierID | CategoryID | Unit | Price |
---|---|---|---|---|---|
1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18 |
2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19 |
3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10 |
4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22 |
5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35 |
以下 SQL 语句查找产品的数量:
【实例】
SELECT COUNT(ProductID)
FROM Products;
注: 不计算 NULL 空值。
以下 SQL 语句查找所有产品的平均价格:
【实例】
SELECT AVG(Price)
FROM Products;
注: 忽略 NULL 空值。
以下是从示例数据库的 "OrderDetails" 表中选择的内容:
OrderDetailID | OrderID | ProductID | Quantity |
---|---|---|---|
1 | 10248 | 11 | 12 |
2 | 10248 | 42 | 10 |
3 | 10248 | 72 | 5 |
4 | 10249 | 14 | 9 |
5 | 10249 | 51 | 40 |
以下 SQL 语句查找 "OrderDetails" 表中 "Quantity" 字段的总和:
【实例】
SELECT SUM(Quantity)
FROM OrderDetails;
注: 忽略 NULL 空值。
LIKE 操作符在 WHERE 子句中用于搜索列中的指定模式。
有两个通配符经常与 LIKE 操作符一起使用:
注: MS Access使用星号 (*) 代替百分号 (%),使用问号 (?) 代替下划线 (_)。百分号和下划线也可以组合使用!
【LIKE 语法】
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
注: 还可以使用 AND 或 OR 运算符组合任意数量的条件。
以下是一些示例,显示了使用'%' 和 '_'通配符的不同 LIKE 运算符:
LIKE Operator | 描述 |
---|---|
WHERE CustomerName LIKE 'a%' | Finds any values that start with "a" |
WHERE CustomerName LIKE '%a' | Finds any values that end with "a" |
WHERE CustomerName LIKE '%or%' | Finds any values that have "or" in any position |
WHERE CustomerName LIKE '_r%' | Finds any values that have "r" in the second position |
WHERE CustomerName LIKE 'a_%' | Finds any values that start with "a" and are at least 2 characters in length |
WHERE CustomerName LIKE 'a__%' | Finds any values that start with "a" and are at least 3 characters in length |
WHERE ContactName LIKE 'a%o' | Finds any values that start with "a" and ends with "o" |
下表显示了样本数据库中完整的客户(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 |
6 | Blauer See Delikatessen | Hanna Moos | Forsterstr. 57 | Mannheim | 68306 | Germany |
7 | Blondel père et fils | Frédérique Citeaux | 24, place Kléber | Strasbourg | 67000 | France |
8 | Bólido Comidas preparadas | Martín Sommer | C/ Araquil, 67 | Madrid | 28023 | Spain |
9 | Bon app' | Laurence Lebihans | 12, rue des Bouchers | Marseille | 13008 | France |
10 | Bottom-Dollar Marketse | Elizabeth Lincoln | 23 Tsawassen Blvd. | Tsawassen | T2F 8M4 | Canada |
11 | B's Beverages | Victoria Ashworth | Fauntleroy Circus | London | EC2 5NT | UK |
12 | Cactus Comidas para llevar | Patricio Simpson | Cerrito 333 | Buenos Aires | 1010 | Argentina |
13 | Centro comercial Moctezuma | Francisco Chang | Sierras de Granada 9993 | México D.F. | 05022 | Mexico |
14 | Chop-suey Chinese | Yang Wang | Hauptstr. 29 | Bern | 3012 | Switzerland |
15 | Comércio Mineiro | Pedro Afonso | Av. dos Lusíadas, 23 | São Paulo | 05432-043 | Brazil |
16 | Consolidated Holdings | Elizabeth Brown | Berkeley Gardens 12 Brewery | London | WX1 6LT | UK |
17 | Drachenblut Delikatessend | Sven Ottlieb | Walserweg 21 | Aachen | 52066 | Germany |
18 | Du monde entier | Janine Labrune | 67, rue des Cinquante Otages | Nantes | 44000 | France |
19 | Eastern Connection | Ann Devon | 35 King George | London | WX3 6FW | UK |
20 | Ernst Handel | Roland Mendel | Kirchgasse 6 | Graz | 8010 | Austria |
21 | Familia Arquibaldo | Aria Cruz | Rua Orós, 92 | São Paulo | 05442-030 | Brazil |
22 | FISSA Fabrica Inter. Salchichas S.A. | Diego Roel | C/ Moralzarzal, 86 | Madrid | 28034 | Spain |
23 | Folies gourmandes | Martine Rancé | 184, chaussée de Tournai | Lille | 59000 | France |
24 | Folk och fä HB | Maria Larsson | Åkergatan 24 | Bräcke | S-844 67 | Sweden |
25 | Frankenversand | Peter Franken | Berliner Platz 43 | München | 80805 | Germany |
26 | France restauration | Carine Schmitt | 54, rue Royale | Nantes | 44000 | France |
27 | Franchi S.p.A. | Paolo Accorti | Via Monte Bianco 34 | Torino | 10100 | Italy |
28 | Furia Bacalhau e Frutos do Mar | Lino Rodriguez | Jardim das rosas n. 32 | Lisboa | 1675 | Portugal |
29 | Galería del gastrónomo | Eduardo Saavedra | Rambla de Cataluña, 23 | Barcelona | 08022 | Spain |
30 | Godos Cocina Típica | José Pedro Freyre | C/ Romero, 33 | Sevilla | 41101 | Spain |
31 | Gourmet Lanchonetes | André Fonseca | Av. Brasil, 442 | Campinas | 04876-786 | Brazil |
32 | Great Lakes Food Market | Howard Snyder | 2732 Baker Blvd. | Eugene | 97403 | USA |
33 | GROSELLA-Restaurante | Manuel Pereira | 5ª Ave. Los Palos Grandes | Caracas | 1081 | Venezuela |
34 | Hanari Carnes | Mario Pontes | Rua do Paço, 67 | Rio de Janeiro | 05454-876 | Brazil |
35 | HILARIÓN-Abastos | Carlos Hernández | Carrera 22 con Ave. Carlos Soublette #8-35 | San Cristóbal | 5022 | Venezuela |
36 | Hungry Coyote Import Store | Yoshi Latimer | City Center Plaza 516 Main St. | Elgin | 97827 | USA |
37 | Hungry Owl All-Night Grocers | Patricia McKenna | 8 Johnstown Road | Cork | Ireland | |
38 | Island Trading | Helen Bennett | Garden House Crowther Way | Cowes | PO31 7PJ | UK |
39 | Königlich Essen | Philip Cramer | Maubelstr. 90 | Brandenburg | 14776 | Germany |
40 | La corne d'abondance | Daniel Tonini | 67, avenue de l'Europe | Versailles | 78000 | France |
41 | La maison d'Asie | Annette Roulet | 1 rue Alsace-Lorraine | Toulouse | 31000 | France |
42 | Laughing Bacchus Wine Cellars | Yoshi Tannamuri | 1900 Oak St. | Vancouver | V3F 2K1 | Canada |
43 | Lazy K Kountry Store | John Steel | 12 Orchestra Terrace | Walla Walla | 99362 | USA |
44 | Lehmanns Marktstand | Renate Messner | Magazinweg 7 | Frankfurt a.M. | 60528 | Germany |
45 | Let's Stop N Shop | Jaime Yorres | 87 Polk St. Suite 5 | San Francisco | 94117 | USA |
46 | LILA-Supermercado | Carlos González | Carrera 52 con Ave. Bolívar #65-98 Llano Largo | Barquisimeto | 3508 | Venezuela |
47 | LINO-Delicateses | Felipe Izquierdo | Ave. 5 de Mayo Porlamar | I. de Margarita | 4980 | Venezuela |
48 | Lonesome Pine Restaurant | Fran Wilson | 89 Chiaroscuro Rd. | Portland | 97219 | USA |
49 | Magazzini Alimentari Riuniti | Giovanni Rovelli | Via Ludovico il Moro 22 | Bergamo | 24100 | Italy |
50 | Maison Dewey | Catherine Dewey | Rue Joseph-Bens 532 | Bruxelles | B-1180 | Belgium |
51 | Mère Paillarde | Jean Fresnière | 43 rue St. Laurent | Montréal | H1J 1C3 | Canada |
52 | Morgenstern Gesundkost | Alexander Feuer | Heerstr. 22 | Leipzig | 04179 | Germany |
53 | North/South | Simon Crowther | South House 300 Queensbridge | London | SW7 1RZ | UK |
54 | Océano Atlántico Ltda. | Yvonne Moncada | Ing. Gustavo Moncada 8585 Piso 20-A | Buenos Aires | 1010 | Argentina |
55 | Old World Delicatessen | Rene Phillips | 2743 Bering St. | Anchorage | 99508 | USA |
56 | Ottilies Käseladen | Henriette Pfalzheim | Mehrheimerstr. 369 | Köln | 50739 | Germany |
57 | Paris spécialités | Marie Bertrand | 265, boulevard Charonne | Paris | 75012 | France |
58 | Pericles Comidas clásicas | Guillermo Fernández | Calle Dr. Jorge Cash 321 | México D.F. | 05033 | Mexico |
59 | Piccolo und mehr | Georg Pipps | Geislweg 14 | Salzburg | 5020 | Austria |
60 | Princesa Isabel Vinhoss | Isabel de Castro | Estrada da saúde n. 58 | Lisboa | 1756 | Portugal |
61 | Que Delícia | Bernardo Batista | Rua da Panificadora, 12 | Rio de Janeiro | 02389-673 | Brazil |
62 | Queen Cozinha | Lúcia Carvalho | Alameda dos Canàrios, 891 | São Paulo | 05487-020 | Brazil |
63 | QUICK-Stop | Horst Kloss | Taucherstraße 10 | Cunewalde | 01307 | Germany |
64 | Rancho grande | Sergio Gutiérrez | Av. del Libertador 900 | Buenos Aires | 1010 | Argentina |
65 | Rattlesnake Canyon Grocery | Paula Wilson | 2817 Milton Dr. | Albuquerque | 87110 | USA |
66 | Reggiani Caseifici | Maurizio Moroni | Strada Provinciale 124 | Reggio Emilia | 42100 | Italy |
67 | Ricardo Adocicados | Janete Limeira | Av. Copacabana, 267 | Rio de Janeiro | 02389-890 | Brazil |
68 | Richter Supermarkt | Michael Holz | Grenzacherweg 237 | Genève | 1203 | Switzerland |
69 | Romero y tomillo | Alejandra Camino | Gran Vía, 1 | Madrid | 28001 | Spain |
70 | Santé Gourmet | Jonas Bergulfsen | Erling Skakkes gate 78 | Stavern | 4110 | Norway |
71 | Save-a-lot Markets | Jose Pavarotti | 187 Suffolk Ln. | Boise | 83720 | USA |
72 | Seven Seas Imports | Hari Kumar | 90 Wadhurst Rd. | London | OX15 4NB | UK |
73 | Simons bistro | Jytte Petersen | Vinbæltet 34 | København | 1734 | Denmark |
74 | Spécialités du monde | Dominique Perrier | 25, rue Lauriston | Paris | 75016 | France |
75 | Split Rail Beer & Ale | Art Braunschweiger | P.O. Box 555 | Lander | 82520 | USA |
76 | Suprêmes délices | Pascale Cartrain | Boulevard Tirou, 255 | Charleroi | B-6000 | Belgium |
77 | The Big Cheese | Liz Nixon | 89 Jefferson Way Suite 2 | Portland | 97201 | USA |
78 | The Cracker Box | Liu Wong | 55 Grizzly Peak Rd. | Butte | 59801 | USA |
79 | Toms Spezialitäten | Karin Josephs | Luisenstr. 48 | Münster | 44087 | Germany |
80 | Tortuga Restaurante | Miguel Angel Paolino | Avda. Azteca 123 | México D.F. | 05033 | Mexico |
81 | Tradição Hipermercados | Anabela Domingues | Av. Inês de Castro, 414 | São Paulo | 05634-030 | Brazil |
82 | Trail's Head Gourmet Provisioners | Helvetius Nagy | 722 DaVinci Blvd. | Kirkland | 98034 | USA |
83 | Vaffeljernet | Palle Ibsen | Smagsløget 45 | Århus | 8200 | Denmark |
84 | Victuailles en stock | Mary Saveley | 2, rue du Commerce | Lyon | 69004 | France |
85 | Vins et alcools Chevalier | Paul Henriot | 59 rue de l'Abbaye | Reims | 51100 | France |
86 | Die Wandernde Kuh | Rita Müller | Adenauerallee 900 | Stuttgart | 70563 | Germany |
87 | Wartian Herkku | Pirkko Koskitalo | Torikatu 38 | Oulu | 90110 | Finland |
88 | Wellington Importadora | Paula Parente | Rua do Mercado, 12 | Resende | 08737-363 | Brazil |
89 | White Clover Markets | Karl Jablonski | 305 - 14th Ave. S. Suite 3B | Seattle | 98128 | USA |
90 | Wilman Kala | Matti Karttunen | Keskuskatu 45 | Helsinki | 21240 | Finland |
91 | Wolski | Zbyszek | ul. Filtrowa 68 | Walla | 01-012 | Poland |
以下 SQL 语句选择 CustomerName 以 "a" 开头的所有客户:
【实例】
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
以下 SQL 语句选择 CustomerName 以 "a" 结尾的所有客户:
【实例】
SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
下面的 SQL 语句选择 CustomerName 中任何位置都有 "or" 的所有客户:
【实例】
SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
下面的 SQL 语句选择 CustomerName 第二个位置有 "r" 的所有客户:
【实例】
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
以下 SQL 语句选择 CustomerName 以 "a" 开头且长度至少为3个字符的所有客户:
【实例】
SELECT * FROM Customers
WHERE CustomerName LIKE 'a__%';
以下 SQL 语句选择 ContactName 以 "a" 开头、以 "o" 结尾的所有客户:
【实例】
SELECT * FROM Customers
WHERE ContactName LIKE 'a%o';
以下 SQL 语句选择 CustomerName 不以 "a" 开头的所有客户:
【实例】
SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%';
通配符可用于替代字符串中的任何其他字符。
通配符与 SQL LIKE 运算符一起使用。LIKE 运算符在 WHERE 子句中用于搜索列中的指定模式。
【MS Access中的通配符】
Symbol | 描述 | 例子 |
---|---|---|
* | 代表零个或多个字符 | bl* finds bl, black, blue, and blob |
? | 代表单个字符 | h?t finds hot, hat, and hit |
[] | 表示括号内的任意单个字符 | h[oa]t finds hot and hat, but not hit |
! | 代表任何不在括号内的字符 | h[!oa]t finds hit, but not hot and hat |
- | 代表一系列字符 | c[a-b]t finds cat and cbt |
# | 表示任意单个数字字符 | 2#5 finds 205, 215, 225, 235, 245, 255, 265, 275, 285, and 295 |
【SQL Server 中的通配符】
Symbol | 描述 | 例子 |
---|---|---|
% | 代表零个或多个字符 | bl% finds bl, black, blue, and blob |
_ | 代表单个字符 | h_t finds hot, hat, and hit |
[] | 表示括号内的任意单个字符 | h[oa]t finds hot and hat, but not hit |
^ | 代表任何不在括号内的字符 | h[^oa]t finds hit, but not hot and hat |
- | 代表一系列字符 | c[a-b]t finds cat and cbt |
所有通配符也可以组合使用!
以下是一些示例,显示了 LIKE 运算符使用 '%' 和 '_' 通配符的不同:
LIKE 操作符 | 描述 |
---|---|
WHERE CustomerName LIKE 'a%' | Finds any values that starts with "a" |
WHERE CustomerName LIKE '%a' | Finds any values that ends with "a" |
WHERE CustomerName LIKE '%or%' | Finds any values that have "or" in any position |
WHERE CustomerName LIKE '_r%' | Finds any values that have "r" in the second position |
WHERE CustomerName LIKE 'a_%_%' | Finds any values that starts with "a" and are at least 3 characters in length |
WHERE ContactName LIKE 'a%o' | Finds any values that starts with "a" and ends with "o" |
下表显示了样本数据库中完整的客户(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 |
6 | Blauer See Delikatessen | Hanna Moos | Forsterstr. 57 | Mannheim | 68306 | Germany |
7 | Blondel père et fils | Frédérique Citeaux | 24, place Kléber | Strasbourg | 67000 | France |
8 | Bólido Comidas preparadas | Martín Sommer | C/ Araquil, 67 | Madrid | 28023 | Spain |
9 | Bon app' | Laurence Lebihans | 12, rue des Bouchers | Marseille | 13008 | France |
10 | Bottom-Dollar Marketse | Elizabeth Lincoln | 23 Tsawassen Blvd. | Tsawassen | T2F 8M4 | Canada |
11 | B's Beverages | Victoria Ashworth | Fauntleroy Circus | London | EC2 5NT | UK |
12 | Cactus Comidas para llevar | Patricio Simpson | Cerrito 333 | Buenos Aires | 1010 | Argentina |
13 | Centro comercial Moctezuma | Francisco Chang | Sierras de Granada 9993 | México D.F. | 05022 | Mexico |
14 | Chop-suey Chinese | Yang Wang | Hauptstr. 29 | Bern | 3012 | Switzerland |
15 | Comércio Mineiro | Pedro Afonso | Av. dos Lusíadas, 23 | São Paulo | 05432-043 | Brazil |
16 | Consolidated Holdings | Elizabeth Brown | Berkeley Gardens 12 Brewery | London | WX1 6LT | UK |
17 | Drachenblut Delikatessend | Sven Ottlieb | Walserweg 21 | Aachen | 52066 | Germany |
18 | Du monde entier | Janine Labrune | 67, rue des Cinquante Otages | Nantes | 44000 | France |
19 | Eastern Connection | Ann Devon | 35 King George | London | WX3 6FW | UK |
20 | Ernst Handel | Roland Mendel | Kirchgasse 6 | Graz | 8010 | Austria |
21 | Familia Arquibaldo | Aria Cruz | Rua Orós, 92 | São Paulo | 05442-030 | Brazil |
22 | FISSA Fabrica Inter. Salchichas S.A. | Diego Roel | C/ Moralzarzal, 86 | Madrid | 28034 | Spain |
23 | Folies gourmandes | Martine Rancé | 184, chaussée de Tournai | Lille | 59000 | France |
24 | Folk och fä HB | Maria Larsson | Åkergatan 24 | Bräcke | S-844 67 | Sweden |
25 | Frankenversand | Peter Franken | Berliner Platz 43 | München | 80805 | Germany |
26 | France restauration | Carine Schmitt | 54, rue Royale | Nantes | 44000 | France |
27 | Franchi S.p.A. | Paolo Accorti | Via Monte Bianco 34 | Torino | 10100 | Italy |
28 | Furia Bacalhau e Frutos do Mar | Lino Rodriguez | Jardim das rosas n. 32 | Lisboa | 1675 | Portugal |
29 | Galería del gastrónomo | Eduardo Saavedra | Rambla de Cataluña, 23 | Barcelona | 08022 | Spain |
30 | Godos Cocina Típica | José Pedro Freyre | C/ Romero, 33 | Sevilla | 41101 | Spain |
31 | Gourmet Lanchonetes | André Fonseca | Av. Brasil, 442 | Campinas | 04876-786 | Brazil |
32 | Great Lakes Food Market | Howard Snyder | 2732 Baker Blvd. | Eugene | 97403 | USA |
33 | GROSELLA-Restaurante | Manuel Pereira | 5ª Ave. Los Palos Grandes | Caracas | 1081 | Venezuela |
34 | Hanari Carnes | Mario Pontes | Rua do Paço, 67 | Rio de Janeiro | 05454-876 | Brazil |
35 | HILARIÓN-Abastos | Carlos Hernández | Carrera 22 con Ave. Carlos Soublette #8-35 | San Cristóbal | 5022 | Venezuela |
36 | Hungry Coyote Import Store | Yoshi Latimer | City Center Plaza 516 Main St. | Elgin | 97827 | USA |
37 | Hungry Owl All-Night Grocers | Patricia McKenna | 8 Johnstown Road | Cork | Ireland | |
38 | Island Trading | Helen Bennett | Garden House Crowther Way | Cowes | PO31 7PJ | UK |
39 | Königlich Essen | Philip Cramer | Maubelstr. 90 | Brandenburg | 14776 | Germany |
40 | La corne d'abondance | Daniel Tonini | 67, avenue de l'Europe | Versailles | 78000 | France |
41 | La maison d'Asie | Annette Roulet | 1 rue Alsace-Lorraine | Toulouse | 31000 | France |
42 | Laughing Bacchus Wine Cellars | Yoshi Tannamuri | 1900 Oak St. | Vancouver | V3F 2K1 | Canada |
43 | Lazy K Kountry Store | John Steel | 12 Orchestra Terrace | Walla Walla | 99362 | USA |
44 | Lehmanns Marktstand | Renate Messner | Magazinweg 7 | Frankfurt a.M. | 60528 | Germany |
45 | Let's Stop N Shop | Jaime Yorres | 87 Polk St. Suite 5 | San Francisco | 94117 | USA |
46 | LILA-Supermercado | Carlos González | Carrera 52 con Ave. Bolívar #65-98 Llano Largo | Barquisimeto | 3508 | Venezuela |
47 | LINO-Delicateses | Felipe Izquierdo | Ave. 5 de Mayo Porlamar | I. de Margarita | 4980 | Venezuela |
48 | Lonesome Pine Restaurant | Fran Wilson | 89 Chiaroscuro Rd. | Portland | 97219 | USA |
49 | Magazzini Alimentari Riuniti | Giovanni Rovelli | Via Ludovico il Moro 22 | Bergamo | 24100 | Italy |
50 | Maison Dewey | Catherine Dewey | Rue Joseph-Bens 532 | Bruxelles | B-1180 | Belgium |
51 | Mère Paillarde | Jean Fresnière | 43 rue St. Laurent | Montréal | H1J 1C3 | Canada |
52 | Morgenstern Gesundkost | Alexander Feuer | Heerstr. 22 | Leipzig | 04179 | Germany |
53 | North/South | Simon Crowther | South House 300 Queensbridge | London | SW7 1RZ | UK |
54 | Océano Atlántico Ltda. | Yvonne Moncada | Ing. Gustavo Moncada 8585 Piso 20-A | Buenos Aires | 1010 | Argentina |
55 | Old World Delicatessen | Rene Phillips | 2743 Bering St. | Anchorage | 99508 | USA |
56 | Ottilies Käseladen | Henriette Pfalzheim | Mehrheimerstr. 369 | Köln | 50739 | Germany |
57 | Paris spécialités | Marie Bertrand | 265, boulevard Charonne | Paris | 75012 | France |
58 | Pericles Comidas clásicas | Guillermo Fernández | Calle Dr. Jorge Cash 321 | México D.F. | 05033 | Mexico |
59 | Piccolo und mehr | Georg Pipps | Geislweg 14 | Salzburg | 5020 | Austria |
60 | Princesa Isabel Vinhoss | Isabel de Castro | Estrada da saúde n. 58 | Lisboa | 1756 | Portugal |
61 | Que Delícia | Bernardo Batista | Rua da Panificadora, 12 | Rio de Janeiro | 02389-673 | Brazil |
62 | Queen Cozinha | Lúcia Carvalho | Alameda dos Canàrios, 891 | São Paulo | 05487-020 | Brazil |
63 | QUICK-Stop | Horst Kloss | Taucherstraße 10 | Cunewalde | 01307 | Germany |
64 | Rancho grande | Sergio Gutiérrez | Av. del Libertador 900 | Buenos Aires | 1010 | Argentina |
65 | Rattlesnake Canyon Grocery | Paula Wilson | 2817 Milton Dr. | Albuquerque | 87110 | USA |
66 | Reggiani Caseifici | Maurizio Moroni | Strada Provinciale 124 | Reggio Emilia | 42100 | Italy |
67 | Ricardo Adocicados | Janete Limeira | Av. Copacabana, 267 | Rio de Janeiro | 02389-890 | Brazil |
68 | Richter Supermarkt | Michael Holz | Grenzacherweg 237 | Genève | 1203 | Switzerland |
69 | Romero y tomillo | Alejandra Camino | Gran Vía, 1 | Madrid | 28001 | Spain |
70 | Santé Gourmet | Jonas Bergulfsen | Erling Skakkes gate 78 | Stavern | 4110 | Norway |
71 | Save-a-lot Markets | Jose Pavarotti | 187 Suffolk Ln. | Boise | 83720 | USA |
72 | Seven Seas Imports | Hari Kumar | 90 Wadhurst Rd. | London | OX15 4NB | UK |
73 | Simons bistro | Jytte Petersen | Vinbæltet 34 | København | 1734 | Denmark |
74 | Spécialités du monde | Dominique Perrier | 25, rue Lauriston | Paris | 75016 | France |
75 | Split Rail Beer & Ale | Art Braunschweiger | P.O. Box 555 | Lander | 82520 | USA |
76 | Suprêmes délices | Pascale Cartrain | Boulevard Tirou, 255 | Charleroi | B-6000 | Belgium |
77 | The Big Cheese | Liz Nixon | 89 Jefferson Way Suite 2 | Portland | 97201 | USA |
78 | The Cracker Box | Liu Wong | 55 Grizzly Peak Rd. | Butte | 59801 | USA |
79 | Toms Spezialitäten | Karin Josephs | Luisenstr. 48 | Münster | 44087 | Germany |
80 | Tortuga Restaurante | Miguel Angel Paolino | Avda. Azteca 123 | México D.F. | 05033 | Mexico |
81 | Tradição Hipermercados | Anabela Domingues | Av. Inês de Castro, 414 | São Paulo | 05634-030 | Brazil |
82 | Trail's Head Gourmet Provisioners | Helvetius Nagy | 722 DaVinci Blvd. | Kirkland | 98034 | USA |
83 | Vaffeljernet | Palle Ibsen | Smagsløget 45 | Århus | 8200 | Denmark |
84 | Victuailles en stock | Mary Saveley | 2, rue du Commerce | Lyon | 69004 | France |
85 | Vins et alcools Chevalier | Paul Henriot | 59 rue de l'Abbaye | Reims | 51100 | France |
86 | Die Wandernde Kuh | Rita Müller | Adenauerallee 900 | Stuttgart | 70563 | Germany |
87 | Wartian Herkku | Pirkko Koskitalo | Torikatu 38 | Oulu | 90110 | Finland |
88 | Wellington Importadora | Paula Parente | Rua do Mercado, 12 | Resende | 08737-363 | Brazil |
89 | White Clover Markets | Karl Jablonski | 305 - 14th Ave. S. Suite 3B | Seattle | 98128 | USA |
90 | Wilman Kala | Matti Karttunen | Keskuskatu 45 | Helsinki | 21240 | Finland |
91 | Wolski | Zbyszek | ul. Filtrowa 68 | Walla | 01-012 | Poland |
以下SQL语句选择城市以 "ber" 开头的所有客户:
【实例】
SELECT * FROM Customers
WHERE City LIKE 'ber%';
以下 SQL 语句选择城市中包含模式 "es" 的所有客户:
【实例】
SELECT * FROM Customers
WHERE City LIKE '%es%';
以下 SQL 语句选择城市以任意字符开头,后跟 "ondon" 的所有客户:
【实例】
SELECT * FROM Customers
WHERE City LIKE '_ondon';
以下 SQL 语句选择城市以"L"开头、后跟任意字符、后跟"n"、后跟任意字符、后跟"on"的所有客户:
【实例】
SELECT * FROM Customers
WHERE City LIKE 'L_n_on';
以下 SQL 语句选择城市以"b"、"s"或"p"开头的所有客户:
【实例】
SELECT * FROM Customers
WHERE City LIKE '[bsp]%';
以下 SQL 语句选择城市以"a"、"b"或"c"开头的所有客户:
【实例】
SELECT * FROM Customers
WHERE City LIKE '[a-c]%';
以下两条SQL语句选择城市不以"b"、"s"或"p"开头的所有客户:
【实例】
SELECT * FROM Customers
WHERE City LIKE '[!bsp]%';
或者:
【实例】
SELECT * FROM Customers
WHERE City NOT LIKE '[bsp]%';
IN 操作符允许我们在 WHERE 子句中规定多个值。
IN 操作符是多个OR条件的简写。
【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);
下表显示了样本数据库中完整的客户(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 |
6 | Blauer See Delikatessen | Hanna Moos | Forsterstr. 57 | Mannheim | 68306 | Germany |
7 | Blondel père et fils | Frédérique Citeaux | 24, place Kléber | Strasbourg | 67000 | France |
8 | Bólido Comidas preparadas | Martín Sommer | C/ Araquil, 67 | Madrid | 28023 | Spain |
9 | Bon app' | Laurence Lebihans | 12, rue des Bouchers | Marseille | 13008 | France |
10 | Bottom-Dollar Marketse | Elizabeth Lincoln | 23 Tsawassen Blvd. | Tsawassen | T2F 8M4 | Canada |
11 | B's Beverages | Victoria Ashworth | Fauntleroy Circus | London | EC2 5NT | UK |
12 | Cactus Comidas para llevar | Patricio Simpson | Cerrito 333 | Buenos Aires | 1010 | Argentina |
13 | Centro comercial Moctezuma | Francisco Chang | Sierras de Granada 9993 | México D.F. | 05022 | Mexico |
14 | Chop-suey Chinese | Yang Wang | Hauptstr. 29 | Bern | 3012 | Switzerland |
15 | Comércio Mineiro | Pedro Afonso | Av. dos Lusíadas, 23 | São Paulo | 05432-043 | Brazil |
16 | Consolidated Holdings | Elizabeth Brown | Berkeley Gardens 12 Brewery | London | WX1 6LT | UK |
17 | Drachenblut Delikatessend | Sven Ottlieb | Walserweg 21 | Aachen | 52066 | Germany |
18 | Du monde entier | Janine Labrune | 67, rue des Cinquante Otages | Nantes | 44000 | France |
19 | Eastern Connection | Ann Devon | 35 King George | London | WX3 6FW | UK |
20 | Ernst Handel | Roland Mendel | Kirchgasse 6 | Graz | 8010 | Austria |
21 | Familia Arquibaldo | Aria Cruz | Rua Orós, 92 | São Paulo | 05442-030 | Brazil |
22 | FISSA Fabrica Inter. Salchichas S.A. | Diego Roel | C/ Moralzarzal, 86 | Madrid | 28034 | Spain |
23 | Folies gourmandes | Martine Rancé | 184, chaussée de Tournai | Lille | 59000 | France |
24 | Folk och fä HB | Maria Larsson | Åkergatan 24 | Bräcke | S-844 67 | Sweden |
25 | Frankenversand | Peter Franken | Berliner Platz 43 | München | 80805 | Germany |
26 | France restauration | Carine Schmitt | 54, rue Royale | Nantes | 44000 | France |
27 | Franchi S.p.A. | Paolo Accorti | Via Monte Bianco 34 | Torino | 10100 | Italy |
28 | Furia Bacalhau e Frutos do Mar | Lino Rodriguez | Jardim das rosas n. 32 | Lisboa | 1675 | Portugal |
29 | Galería del gastrónomo | Eduardo Saavedra | Rambla de Cataluña, 23 | Barcelona | 08022 | Spain |
30 | Godos Cocina Típica | José Pedro Freyre | C/ Romero, 33 | Sevilla | 41101 | Spain |
31 | Gourmet Lanchonetes | André Fonseca | Av. Brasil, 442 | Campinas | 04876-786 | Brazil |
32 | Great Lakes Food Market | Howard Snyder | 2732 Baker Blvd. | Eugene | 97403 | USA |
33 | GROSELLA-Restaurante | Manuel Pereira | 5ª Ave. Los Palos Grandes | Caracas | 1081 | Venezuela |
34 | Hanari Carnes | Mario Pontes | Rua do Paço, 67 | Rio de Janeiro | 05454-876 | Brazil |
35 | HILARIÓN-Abastos | Carlos Hernández | Carrera 22 con Ave. Carlos Soublette #8-35 | San Cristóbal | 5022 | Venezuela |
36 | Hungry Coyote Import Store | Yoshi Latimer | City Center Plaza 516 Main St. | Elgin | 97827 | USA |
37 | Hungry Owl All-Night Grocers | Patricia McKenna | 8 Johnstown Road | Cork | Ireland | |
38 | Island Trading | Helen Bennett | Garden House Crowther Way | Cowes | PO31 7PJ | UK |
39 | Königlich Essen | Philip Cramer | Maubelstr. 90 | Brandenburg | 14776 | Germany |
40 | La corne d'abondance | Daniel Tonini | 67, avenue de l'Europe | Versailles | 78000 | France |
41 | La maison d'Asie | Annette Roulet | 1 rue Alsace-Lorraine | Toulouse | 31000 | France |
42 | Laughing Bacchus Wine Cellars | Yoshi Tannamuri | 1900 Oak St. | Vancouver | V3F 2K1 | Canada |
43 | Lazy K Kountry Store | John Steel | 12 Orchestra Terrace | Walla Walla | 99362 | USA |
44 | Lehmanns Marktstand | Renate Messner | Magazinweg 7 | Frankfurt a.M. | 60528 | Germany |
45 | Let's Stop N Shop | Jaime Yorres | 87 Polk St. Suite 5 | San Francisco | 94117 | USA |
46 | LILA-Supermercado | Carlos González | Carrera 52 con Ave. Bolívar #65-98 Llano Largo | Barquisimeto | 3508 | Venezuela |
47 | LINO-Delicateses | Felipe Izquierdo | Ave. 5 de Mayo Porlamar | I. de Margarita | 4980 | Venezuela |
48 | Lonesome Pine Restaurant | Fran Wilson | 89 Chiaroscuro Rd. | Portland | 97219 | USA |
49 | Magazzini Alimentari Riuniti | Giovanni Rovelli | Via Ludovico il Moro 22 | Bergamo | 24100 | Italy |
50 | Maison Dewey | Catherine Dewey | Rue Joseph-Bens 532 | Bruxelles | B-1180 | Belgium |
51 | Mère Paillarde | Jean Fresnière | 43 rue St. Laurent | Montréal | H1J 1C3 | Canada |
52 | Morgenstern Gesundkost | Alexander Feuer | Heerstr. 22 | Leipzig | 04179 | Germany |
53 | North/South | Simon Crowther | South House 300 Queensbridge | London | SW7 1RZ | UK |
54 | Océano Atlántico Ltda. | Yvonne Moncada | Ing. Gustavo Moncada 8585 Piso 20-A | Buenos Aires | 1010 | Argentina |
55 | Old World Delicatessen | Rene Phillips | 2743 Bering St. | Anchorage | 99508 | USA |
56 | Ottilies Käseladen | Henriette Pfalzheim | Mehrheimerstr. 369 | Köln | 50739 | Germany |
57 | Paris spécialités | Marie Bertrand | 265, boulevard Charonne | Paris | 75012 | France |
58 | Pericles Comidas clásicas | Guillermo Fernández | Calle Dr. Jorge Cash 321 | México D.F. | 05033 | Mexico |
59 | Piccolo und mehr | Georg Pipps | Geislweg 14 | Salzburg | 5020 | Austria |
60 | Princesa Isabel Vinhoss | Isabel de Castro | Estrada da saúde n. 58 | Lisboa | 1756 | Portugal |
61 | Que Delícia | Bernardo Batista | Rua da Panificadora, 12 | Rio de Janeiro | 02389-673 | Brazil |
62 | Queen Cozinha | Lúcia Carvalho | Alameda dos Canàrios, 891 | São Paulo | 05487-020 | Brazil |
63 | QUICK-Stop | Horst Kloss | Taucherstraße 10 | Cunewalde | 01307 | Germany |
64 | Rancho grande | Sergio Gutiérrez | Av. del Libertador 900 | Buenos Aires | 1010 | Argentina |
65 | Rattlesnake Canyon Grocery | Paula Wilson | 2817 Milton Dr. | Albuquerque | 87110 | USA |
66 | Reggiani Caseifici | Maurizio Moroni | Strada Provinciale 124 | Reggio Emilia | 42100 | Italy |
67 | Ricardo Adocicados | Janete Limeira | Av. Copacabana, 267 | Rio de Janeiro | 02389-890 | Brazil |
68 | Richter Supermarkt | Michael Holz | Grenzacherweg 237 | Genève | 1203 | Switzerland |
69 | Romero y tomillo | Alejandra Camino | Gran Vía, 1 | Madrid | 28001 | Spain |
70 | Santé Gourmet | Jonas Bergulfsen | Erling Skakkes gate 78 | Stavern | 4110 | Norway |
71 | Save-a-lot Markets | Jose Pavarotti | 187 Suffolk Ln. | Boise | 83720 | USA |
72 | Seven Seas Imports | Hari Kumar | 90 Wadhurst Rd. | London | OX15 4NB | UK |
73 | Simons bistro | Jytte Petersen | Vinbæltet 34 | København | 1734 | Denmark |
74 | Spécialités du monde | Dominique Perrier | 25, rue Lauriston | Paris | 75016 | France |
75 | Split Rail Beer & Ale | Art Braunschweiger | P.O. Box 555 | Lander | 82520 | USA |
76 | Suprêmes délices | Pascale Cartrain | Boulevard Tirou, 255 | Charleroi | B-6000 | Belgium |
77 | The Big Cheese | Liz Nixon | 89 Jefferson Way Suite 2 | Portland | 97201 | USA |
78 | The Cracker Box | Liu Wong | 55 Grizzly Peak Rd. | Butte | 59801 | USA |
79 | Toms Spezialitäten | Karin Josephs | Luisenstr. 48 | Münster | 44087 | Germany |
80 | Tortuga Restaurante | Miguel Angel Paolino | Avda. Azteca 123 | México D.F. | 05033 | Mexico |
81 | Tradição Hipermercados | Anabela Domingues | Av. Inês de Castro, 414 | São Paulo | 05634-030 | Brazil |
82 | Trail's Head Gourmet Provisioners | Helvetius Nagy | 722 DaVinci Blvd. | Kirkland | 98034 | USA |
83 | Vaffeljernet | Palle Ibsen | Smagsløget 45 | Århus | 8200 | Denmark |
84 | Victuailles en stock | Mary Saveley | 2, rue du Commerce | Lyon | 69004 | France |
85 | Vins et alcools Chevalier | Paul Henriot | 59 rue de l'Abbaye | Reims | 51100 | France |
86 | Die Wandernde Kuh | Rita Müller | Adenauerallee 900 | Stuttgart | 70563 | Germany |
87 | Wartian Herkku | Pirkko Koskitalo | Torikatu 38 | Oulu | 90110 | Finland |
88 | Wellington Importadora | Paula Parente | Rua do Mercado, 12 | Resende | 08737-363 | Brazil |
89 | White Clover Markets | Karl Jablonski | 305 - 14th Ave. S. Suite 3B | Seattle | 98128 | USA |
90 | Wilman Kala | Matti Karttunen | Keskuskatu 45 | Helsinki | 21240 | Finland |
91 | Wolski | Zbyszek | ul. Filtrowa 68 | Walla | 01-012 | Poland |
以下 SQL 语句选择位于 "Germany", "France", "UK" 的所有客户:
【实例】
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
以下 SQL 语句选择不在 "Germany", "France" or "UK" 的所有客户:
【实例】
SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
以下 SQL 语句选择与供应商来自相同国家/地区的所有客户:
【实例】
SELECT * FROM Customers
WHERE Country IN (SELECT Country FROM Suppliers);
BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围。这些值可以是数值、文本或者日期。
BETWEEN 操作符包括开始值和结束值。
【BETWEEN 语法】
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
以下是从示例数据库的 产品(Products) 表中选择的内容:
ProductID | ProductName | SupplierID | CategoryID | Unit | Price |
---|---|---|---|---|---|
1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18 |
2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19 |
3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10 |
4 | Chef Anton's Cajun Seasoning | 1 | 2 | 48 - 6 oz jars | 22 |
5 | Chef Anton's Gumbo Mix | 1 | 2 | 36 boxes | 21.35 |
以下 SQL 语句选择价格介于 10 和 20 之间的所有产品:
【实例】
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
要显示上一示例范围之外的产品,请使用 NOT BETWEEN:
【实例】
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
下面的 SQL 语句选择价格介于 10 和 20 之间的所有产品。此外请勿显示 CategoryID 为1、2或3的产品:
【实例】
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20
AND CategoryID NOT IN (1,2,3);
以下 SQL 语句选择所有产品,其产品名称介于 Carnavon Tigers 和 Mozzarella di Giovanni 之间:
【实例】
SELECT * FROM Products
WHERE ProductName BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni'
ORDER BY ProductName;
以下SQL语句选择所有产品,产品名称介于 Carnarvon Tigers 和 Chef Anton's Cajun Seasoning 之间:
【实例】
SELECT * FROM Products
WHERE ProductName BETWEEN "Carnarvon Tigers" AND "Chef Anton's Cajun Seasoning"
ORDER BY ProductName;
以下SQL语句选择了所有产品,其产品名称不在 Carnarvon Tigers 和 Mozzarella di Giovanni 之间:
【实例】
SELECT * FROM Products
WHERE ProductName NOT BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni'
ORDER BY ProductName;
以下是从示例数据库的 "订单(Orders)" 表中选择的内容:
OrderID | CustomerID | EmployeeID | OrderDate | ShipperID |
---|---|---|---|---|
10248 | 90 | 5 | 7/4/1996 | 3 |
10249 | 81 | 6 | 7/5/1996 | 1 |
10250 | 34 | 4 | 7/8/1996 | 2 |
10251 | 84 | 3 | 7/9/1996 | 1 |
10252 | 76 | 4 | 7/10/1996 | 2 |
以下 SQL 语句选择 OrderDate 介于 "1996年7月1日" 和 "1996年7月31日" 之间的所有订单:
【实例】
SELECT * FROM Orders
WHERE OrderDate BETWEEN #01/07/1996# AND #31/07/1996#;
或者:
SELECT * FROM Orders
WHERE OrderDate BETWEEN '1996-07-01' AND '1996-07-31';
SQL 别名用于为表或表中的列提供临时名称。
别名通常用于提高列名的可读性。
别名仅在查询期间有效。
【列的 SQL Alias 语法】
SELECT column_name AS alias_name
FROM table_name;
【表的 SQL Alias 语法】
SELECT column_name(s)
FROM table_name AS alias_name;
以下是从 "Customers" 表中选择的内容:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
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 |
以及从 "Orders" 表中选择的内容:
OrderID | CustomerID | EmployeeID | OrderDate | ShipperID |
---|---|---|---|---|
10354 | 58 | 8 | 1996-11-14 | 3 |
10355 | 4 | 6 | 1996-11-15 | 1 |
10356 | 86 | 6 | 1996-11-18 | 2 |
以下 SQL 语句创建两个别名,一个用于 CustomerID 列,另一个用于 CustomerName 列:
【实例】
SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;
下面的SQL语句创建两个别名,一个用于CustomerName列,另一个用于ContactName列。
注: 如果别名包含空格,则需要双引号或方括号。
【实例】
SELECT CustomerName AS Customer, ContactName AS [Contact Person]
FROM Customers;
以下 SQL 语句创建一个名为 "Address" 的别名,该别名由四列(Address、PostalCode、City和Country)组成:
【实例】
SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country AS Address
FROM Customers;
注: 要使上述 SQL 语句在 MySQL 中工作,请使用以下命令:
SELECT CustomerName, CONCAT(Address,', ',PostalCode,', ',City,', ',Country) AS Address
FROM Customers;
下面的 SQL 语句选择 CustomerID=4(Around the Horn)的客户的所有订单。我们使用 "Customers" 和 "Orders" 表,并分别为它们提供表别名 "c" 和 "o"(这里我们使用别名来缩短 SQL):
【实例】
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName='Around the Horn' AND c.CustomerID=o.CustomerID;
以下 SQL 语句与上面相同,但没有别名:
【实例】
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName='Around the Horn' AND Customers.CustomerID=Orders.CustomerID;
在下面的情况下,使用别名很有用:
JOIN SQL 用于根据两个或多个表之间的一个相关列合并两个或多个表中的行。
请看 "Orders" 表:
OrderID | CustomerID | OrderDate |
---|---|---|
10308 | 2 | 1996-09-18 |
10309 | 37 | 1996-09-19 |
10310 | 77 | 1996-09-20 |
然后,请看 "Customers" 表:
CustomerID | CustomerName | ContactName | Country |
---|---|---|---|
1 | Alfreds Futterkiste | Maria Anders | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mexico |
请注意,"Customers" 表中的 "CustomerID" 列指的是 "Customers" 表中的 "CustomerID"。上面两个表之间的关系是 "CustomerID" 列。
然后,我们可以创建以下 SQL 语句(包含 INNER JOIN),用于选择两个表中具有匹配值的记录:
【实例】
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
将产生如下结果:
OrderID | CustomerName | OrderDate |
---|---|---|
10308 | Ana Trujillo Emparedados y helados | 9/18/1996 |
10365 | Antonio Moreno Taquería | 11/27/1996 |
10383 | Around the Horn | 12/16/1996 |
10355 | Around the Horn | 11/15/1996 |
10278 | Berglunds snabbköp | 8/12/1996 |
下面列出了您可以使用的 JOIN 类型,以及它们之间的差异。