T Sql 简明教程
T-SQL - WHERE Clause
MS SQL Server WHERE 子句用于在从单个表获取数据或与多个表连接时指定条件。
The MS SQL Server WHERE clause is used to specify a condition while fetching the data from single table or joining with multiple tables.
如果满足给定条件,则它仅从表中返回一个特定值。您将必须使用 WHERE 子句来筛选记录并仅获取必需的记录。
If the given condition is satisfied, only then it returns a specific value from the table. You will have to use WHERE clause to filter the records and fetch only necessary records.
WHERE 子句不仅用于 SELECT 语句中,而且还用于 UPDATE、DELETE 语句中,我们将在后续章节中研究这些语句。
The WHERE clause is not only used in SELECT statement, but it is also used in UPDATE, DELETE statement, etc., which we would examine in subsequent chapters.
Syntax
以下是带有 WHERE 子句的 SELECT 语句的基本语法−
Following is the basic syntax of SELECT statement with WHERE clause −
SELECT column1, column2, columnN
FROM table_name
WHERE [condition]
您可以使用 >、<、=、LIKE、NOT 等比较或逻辑运算符指定条件。以下示例将阐明此概念。
You can specify a condition using comparison or logical operators like >, <, =, LIKE, NOT, etc. The following example will make this concept clear.
Example
考虑包含以下记录的 CUSTOMERS 表:
Consider the CUSTOMERS table having the following records −
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
以下命令是一个示例,它将从 CUSTOMERS 表中获取 ID、Name 和 Salary 字段,其中工资大于 2000。
Following command is an example which would fetch ID, Name and Salary fields from the CUSTOMERS table where salary is greater than 2000.
SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY > 2000;
以上命令将生成以下输出。
The above command will produce the following output.
ID NAME SALARY
4 Chaitali 6500.00
5 Hardik 8500.00
6 Komal 4500.00
7 Muffy 10000.00
以下命令是一个示例,它将为名为“Hardik”的客户从 CUSTOMERS 表中获取 ID、Name 和 Salary 字段。需要注意的是,所有字符串都应放在单引号('')内,而数字值应不带任何引号,如上例所示−
Following command is an example, which would fetch ID, Name and Salary fields from the CUSTOMERS table for a customer with the name ‘Hardik’. It is important to note that all the strings should be given inside single quotes ('') whereas numeric values should be given without any quote as in the above example −
SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE NAME = 'Hardik';
以上命令将生成以下输出。
The above command will produce the following output.
ID NAME SALARY
5 Hardik 8500.00