Sql简介 专题
专题目录
您的位置:database > Sql简介专题 > SQL Or运算符
SQL Or运算符
作者:--    发布时间:2019-11-20

在本教程中,您将学习如何使用sql or运算符组合两个布尔表达式。

1. sql or运算符简介

sql or是一个逻辑运算符,它组合两个布尔表达式。 sql or运算符计算结果返回truefalse,具体取决于表达式的结果。

or运算符通常用于在selectupdatedelete语句的where子句中,以形成灵活的条件。

以下说明了sql or运算符的语法:

select
    column1,
    column2,
        ...
from
    table1
where
    expression1
or expression2;

下表显示了比较truefalsenull值时or运算符的结果。

true false null
true true true true
false true false null
null true null null

请注意,如果任一表达式为true,则or运算符始终返回true

如果数据库系统支持短路功能,则只要一个表达式为真,or运算符就会停止评估条件的其余部分。

注意,短路功能可以帮助数据库系统通过在确定结果后立即中止处理逻辑表达式的剩余部分来节省cpu计算。 有关短路功能的更多信息,请查看sql and运算符教程。

or运算符与and运算符一起使用时,数据库系统会在and运算符之后计算or运算符。 它叫作优先规则。 但是,可以使用括号更改评估顺序。

2. sql or运算符示例

我们将使用示例数据库中的employees表来演示or运算符。

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

以下声明查找了1997年或1998年加入公司的所有员工。

select
    first_name, last_name, hire_date
from
    employees
where
    year (hire_date) = 1997 or year (hire_date) = 1998
order by
    first_name, last_name;

执行上面示例代码,得到以下结果 -

要查找1997年或1998年加入公司并在部门id为3中工作的所有员工,可以使用andor运算符,如下所示:

select
    first_name, last_name, hire_date, department_id
from
    employees
where
    department_id = 3
and (
    year (hire_date) = 1997
    or year (hire_date) = 1998
)
order by
    first_name, last_name;

执行上面查询语句,得到以下结果 -

如果不使用括号,查询将检索1997年加入公司并在部门id为3工作的员工或1998年加入公司的员工(不限部门是哪个)。

这是因为数据库系统在and运算符之后计算or运算符。

select
    first_name, last_name, hire_date, department_id
from
    employees
where
    department_id = 3
and year (hire_date) = 1997
or year (hire_date) = 1998
order by
    first_name, last_name;

执行上面查询语句,得到以下结果 -

如果查询使用许多or运算符,则将难以阅读。 要使查询更具可读性,可以使用in运算符

例如,以下查询查找在1990年或1999年或2000年加入公司的所有员工。

select
    first_name,
    last_name,
    hire_date
from
    employees
where
    year (hire_date) = 2000
or year (hire_date) = 1999
or year (hire_date) = 1990;

可以通过in运算符替换or运算符,如下所示:

select
    first_name,
    last_name,
    hire_date
from
    employees
where
    year (hire_date) in (1990, 1999, 2000)
order by
    hire_date;

执行上面示例代码,得到以下结果 -

+------------+-----------+------------+
| first_name | last_name | hire_date  |
+------------+-----------+------------+
| alexander  | lee       | 1990-01-03 |
| diana      | chen      | 1999-02-07 |
| kimberely  | yang      | 1999-05-24 |
| karen      | zhang     | 1999-08-10 |
| min        | su        | 1999-12-07 |
| charles    | yang      | 2000-01-04 |
+------------+-----------+------------+
6 rows in set

在本教程中,您已经学习了如何使用sql or运算符组合两个布尔表达式以形成灵活的条件。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册