AND语法
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR语法
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT语法
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
演示数据库
在本教程中,我们将使用著名的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
AND 运算符实例
以下SQL语句从 "Customers" 表中选择其国家为 "Germany" 、其城市为"Berlin" 的所有客户:
实例
SELECT * FROM Customers
WHERE Country='Germany'
AND City='Berlin';
OR 运算符实例
以下SQL语句选择城市为“Berlin”或“München”的“Customers”的所有字段:
实例
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
NOT 运算符实例
以下SQL语句选择国家不是 "Germany"的"Customers"的所有字段:
SELECT * FROM Customers
WHERE NOT Country='Germany';