SQL WHERE

SQL WHERE 子句用来过滤查询结果,可以用于 SQL Select, update, delete, insert 等语句。

SQL WHERE 语法

SELECT *
FROM 表名
WHERE ColumnName operator Value

SQL WHERE 范例

数据表: Employees

EmployeeId FirstName LastName Department Salary
203 Mazojys Fxoj Finance 78000
204 Jozzh Lnanyo Finance 45800
205 Syllauu Dfaafk Finance 57000
206 Gecrrcc Srlkrt Finance 62000
302 Jssme Bdnaa Development 75000
303 Dnnaao Errllov Development 55000
304 Tyoysww Osk Development 49000

下面是不同操作符的例子:

= (等于)

SELECT *
FROM Employees
WHERE FirstName = 'Jssme'

<> (不等于)

SELECT *
FROM Employees
WHERE FirstName <> 'Jssme'

> (大于)

SELECT *
FROM Employees
WHERE Salary > 55000

< (小于)

SELECT *
FROM Employees
WHERE Salary < 55000

>= (大于或等于)

SELECT *
FROM Employees
WHERE Salary >= 55000

<= (小于或等于)

SELECT *
FROM Employees
WHERE Salary <= 55000

BETWEEN (在一个范围之间)

SELECT *
FROM Employees
WHERE Salary BETWEEN 55000 AND 75000

LIKE (模糊查询)

SELECT *
FROM Employees
WHERE FirstName LIKE 'Jss%'

IN (在一个数据集中)

SELECT *
FROM Employees
WHERE EmployeeId IN (203, 204, 302)

我们也可以将这些操作符联在一起:

SELECT *
FROM Employees
WHERE (FirstName LIKE 'J%'
       AND Salary > 55000
      )
      OR EmployeeId = 304