Sqlite 简明教程

SQLite - AND & OR Operators

SQLite ANDOR 运算符用于编译多个条件以缩小 SQLite 语句中选定数据的范围。这两个运算符称为 conjunctive operators

SQLite AND & OR operators are used to compile multiple conditions to narrow down the selected data in an SQLite statement. These two operators are called conjunctive operators.

这些运算符提供了一种在同一 SQLite 语句中用不同的运算符进行多次比较的方法。

These operators provide a means to make multiple comparisons with different operators in the same SQLite statement.

The AND Operator

AND 运算符允许在 SQLite 语句的 WHERE 子句中存在多个条件。在使用 AND 运算符时,当所有条件都为真时,完整的条件将被假定为真。例如,只有当 condition1 和 condition2 均为真时,才为 true。

The AND operator allows the existence of multiple conditions in a SQLite statement’s WHERE clause. While using AND operator, complete condition will be assumed true when all the conditions are true. For example, [condition1] AND [condition2] will be true only when both condition1 and condition2 are true.

Syntax

以下是带 WHERE 子句的 AND 运算符的基本语法。

Following is the basic syntax of AND operator with WHERE clause.

SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] AND [condition2]...AND [conditionN];

你可以使用 AND 运算符合并 N 个条件。对于 SQLite 语句要执行的操作,无论其为事务还是查询,由 AND 分隔的所有条件都必须为 TRUE。

You can combine N number of conditions using AND operator. For an action to be taken by the SQLite statement, whether it be a transaction or query, all conditions separated by the AND must be TRUE.

Example

考虑具有以下记录的 COMPANY 表 -

Consider COMPANY table with the following records −

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

以下 SELECT 语句列出了 AGE 大于或等于 25 AND 薪水大于或等于 65000.00 的所有记录。

Following SELECT statement lists down all the records where AGE is greater than or equal to 25 AND salary is greater than or equal to 65000.00.

sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000;

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0

The OR Operator

OR 运算符也用于在 SQLite 语句的 WHERE 子句中合并多个条件。在使用 OR 运算符时,当至少满足其中任何一个条件时,完整的条件将被假定为真。例如,当 condition1 或 condition2 为真时,[condition1] OR [condition2] 为真。

The OR operator is also used to combine multiple conditions in a SQLite statement’s WHERE clause. While using OR operator, complete condition will be assumed true when at least any of the conditions is true. For example, [condition1] OR [condition2] will be true if either condition1 or condition2 is true.

Syntax

以下是带有 WHERE 子句的 OR 运算符的基本语法。

Following is the basic syntax of OR operator with WHERE clause.

SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] OR [condition2]...OR [conditionN]

你可以使用 OR 运算符合并 N 个条件。对于 SQLite 语句要执行的操作,无论其为事务还是查询,由 OR 分隔的所有条件中只需任意一个为 TRUE。

You can combine N number of conditions using OR operator. For an action to be taken by the SQLite statement, whether it be a transaction or query, only any ONE of the conditions separated by the OR must be TRUE.

Example

考虑具有以下记录的 COMPANY 表。

Consider COMPANY table with the following records.

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

以下 SELECT 语句列出了 AGE 大于或等于 25 OR 薪水大于或等于 65000.00 的所有记录。

Following SELECT statement lists down all the records where AGE is greater than or equal to 25 OR salary is greater than or equal to 65000.00.

sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000;

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0