在本教程中,您将学习如何使用sql where
子句根据指定的条件筛选表中的行记录。
要从表中选择某些行,请在select
语句中使用where
子句。 以下说明了select
语句中where
子句的语法:
select
column1, column2, ...
from
table
where
condition;
where
子句紧跟在from
子句之后出现。 where
子句包含一个或多个逻辑表达式,用于计算表中的每一行。 如果条件计算为true
,则满足条件的行记录将包含在结果集中; 否则,它将被排除在外。
请注意,sql具有三值逻辑,即true
,false
和unknown
。 这意味着如果某行导致条件计算为false
或null
,则不会返回该行。
请注意,where
子句后面的逻辑表达式也称为谓词。 可以使用各种运算符来形成where
子句中使用的行选择条件。
下表显示了sql比较运算符:
编号 | 运算符 | 含义 |
---|---|---|
1 | = |
等于 |
2 | <> 或!= |
不等于 |
3 | < |
小于 |
4 | > |
大于 |
5 | <= |
小于或等于 |
6 | >= |
大于或等于 |
要形成一个简单的表达式,可以使用上面的一个运算符和两个操作数,这两个操作数可以是一侧的列名,另一侧是文字值,例如:
salary > 1000
表示:“工资是否大于1000?”。或者可以在运算符的两侧使用列名称,例如:
min_salary < max_salary
这个表达式提出了另一个问题:“最低工资是否低于最高薪水?”。
在表达式中使用的文字值可以是数字,字符,日期和时间,具体取决于使用的格式:
100
,200.5
'100'
,'john dan'
。'yyyy-mm-dd'
格式来存储日期数据。'hh:mm:ss'
来存储时间数据。除select
语句外,还可以在update或delete语句中使用where
子句指定要更新或删除的行。
我们将使用employees
表来演示如何使用where
子句从表中选择数据。
mysql> desc employees;
+---------------+--------------+------+-----+---------+----------------+
| field | type | null | key | default | extra |
+---------------+--------------+------+-----+---------+----------------+
| employee_id | int(11) | no | pri | null | auto_increment |
| first_name | varchar(20) | yes | | null | |
| last_name | varchar(25) | no | | null | |
| email | varchar(100) | no | | null | |
| phone_number | varchar(20) | yes | | null | |
| hire_date | date | no | | null | |
| job_id | int(11) | no | mul | null | |
| salary | decimal(8,2) | no | | null | |
| manager_id | int(11) | yes | mul | null | |
| department_id | int(11) | yes | mul | null | |
+---------------+--------------+------+-----+---------+----------------+
10 rows in set
带有数字比较示例的sql where子句
以下查询查找薪水大于14000
的员工,并根据薪水按降序对结果集进行排序。
select
employee_id, first_name, last_name, salary
from
employees
where
salary > 14000
order by
salary desc;
执行上面示例代码,得到以下结果 -
以下查询查找在部门id为5
中工作的所有员工。
select
employee_id, first_name, last_name, department_id
from
employees
where
department_id = 5
order by
first_name;
执行上面示例代码,得到以下结果 -
带有字符示例的sql where子句
sql不区分大小写。 但是,当涉及比较中的值时,它区分大小写。 例如,以下查询查找姓氏(last_name
)为zhang
的员工。
select
employee_id, first_name, last_name
from
employees
where
last_name = 'zhang';
执行上面示例代码,得到以下结果 -
mysql> select
employee_id, first_name, last_name
from
employees
where
last_name = 'zhang';
+-------------+------------+-----------+
| employee_id | first_name | last_name |
+-------------+------------+-----------+
| 116 | shelli | zhang |
| 117 | sigal | zhang |
| 118 | guy | zhang |
| 119 | karen | zhang |
+-------------+------------+-----------+
4 rows in set
但是,如果查询值是:zhang
或zhang
,则不会返回任何行记录。
带有日期示例的sql where子句
要获得1999年1月1日之后加入公司的所有员工,请使用以下查询:
select
employee_id, first_name, last_name, hire_date
from
employees
where
hire_date >= '1999-01-01'
order by
hire_date desc;
执行上面示例代码,得到以下结果 -
如果想查询1999年加入公司的员工,可以通过以下几种方式实现:
以下语句说明了如何使用第一种方式:
select
employee_id, first_name, last_name, hire_date
from
employees
where
year (hire_date) = 1999
order by
hire_date desc;
执行上面示例代码,得到以下结果 -
在本教程中,我们向您展示了如何使用sql where
子句根据指定的条件过滤数据。