WHERE (Transact-SQL)WHERE (Transact-SQL)

适用对象:是SQL Server 是Azure SQL 数据库 是Azure Synapse Analytics (SQL DW) 是并行数据仓库 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) yesParallel Data Warehouse

指定查询返回的行的搜索条件。Specifies the search condition for the rows returned by the query.

主题链接图标 TRANSACT-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

[ WHERE <search_condition> ]  

参数Arguments

< search_condition > 定义要返回的行应满足的条件 。< search_condition > Defines the condition to be met for the rows to be returned. 对搜索条件中可以包含的谓词数量没有限制。There is no limit to the number of predicates that can be included in a search condition. 有关搜索条件和谓词的详细信息,请参阅搜索条件 (Transact-SQL)For more information about search conditions and predicates, see Search Condition (Transact-SQL).

示例Examples

下面的示例演示如何在 WHERE 子句中使用某些常见的搜索条件。The following examples show how to use some common search conditions in the WHERE clause.

A.A. 使用简单等式查找行Finding a row by using a simple equality

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE LastName = 'Smith' ;  

B.B. 查找包含作为字符串一部分的值的行Finding rows that contain a value as part of a string

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE LastName LIKE ('%Smi%');  

C.C. 使用比较运算符查找行Finding rows by using a comparison operator

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE EmployeeKey  <= 500;  

D.D. 查找满足三个条件当中的任何一个条件的行Finding rows that meet any of three conditions

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE EmployeeKey = 1 OR EmployeeKey = 8 OR EmployeeKey = 12;  

E.E. 查找必须满足若干个条件的行Finding rows that must meet several conditions

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE EmployeeKey <= 500 AND LastName LIKE '%Smi%' AND FirstName LIKE '%A%';  

F.F. 查找位于值列表中的行Finding rows that are in a list of values

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE LastName IN ('Smith', 'Godfrey', 'Johnson');  

G.G. 查找含有两个值之间的一个值的行Finding rows that have a value between two values

-- Uses AdventureWorksDW  
  
SELECT EmployeeKey, LastName  
FROM DimEmployee  
WHERE EmployeeKey Between 100 AND 200;  

另请参阅See Also

DELETE (Transact-SQL) DELETE (Transact-SQL)
谓词(Transact-SQL) Predicates (Transact-SQL)
搜索条件 (Transact-SQL) Search Condition (Transact-SQL)
SELECT (Transact-SQL) SELECT (Transact-SQL)
UPDATE (Transact-SQL) UPDATE (Transact-SQL)
MERGE (Transact-SQL)MERGE (Transact-SQL)