Postgresql 简明教程
AND and OR Conjunctive Operators
PostgreSQL AND 和 OR 运算符用于组合多个条件,以缩小 PostgreSQL 语句中选择的数据范围。这两个运算符称为结合运算符。
The PostgreSQL AND and OR operators are used to combine multiple conditions to narrow down selected data in a PostgreSQL statement. These two operators are called conjunctive operators.
这些运算符提供了一种在同一 PostgreSQL 语句中使用不同运算符进行多个比较的方法。
These operators provide a means to make multiple comparisons with different operators in the same PostgreSQL statement.
The AND Operator
AND 运算符允许在 PostgreSQL 语句的 WHERE 子句中存在多个条件。在使用 AND 运算符时,当所有条件都为 true 时,将假定完整条件为 true。例如,仅当 condition1 和 condition2 都为 true 时,[condition1] AND [condition2] 才为 true。
The AND operator allows the existence of multiple conditions in a PostgreSQL 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
AND 运算符与 WHERE 子句的基本语法如下 −
The basic syntax of AND operator with WHERE clause is as follows −
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] AND [condition2]...AND [conditionN];
您可以使用 AND 运算符组合 N 个条件。PostgreSQL 语句执行某个操作(无论是事务还是查询),AND 分隔的所有条件都必须为 TRUE。
You can combine N number of conditions using AND operator. For an action to be taken by the PostgreSQL statement, whether it be a transaction or query, all conditions separated by the AND must be TRUE.
Example
考虑 COMPANY 表具有以下记录:
Consider the table COMPANY having records as follows −
testdb# select * from COMPANY;
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall| 45000
7 | James | 24 | Houston | 10000
(7 rows)
以下 SELECT 语句列出了所有 AGE 大于或等于 25 AND 且 salary 大于或等于 65000.00 的记录 −
The 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 −
testdb=# SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000;
以上给出的 PostgreSQL 语句将产生以下结果 -
The above given PostgreSQL statement will produce the following result −
id | name | age | address | salary
----+-------+-----+------------+--------
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
(2 rows)
The OR Operator
OR 运算符也用于组合 PostgreSQL 语句的 WHERE 子句中的多个条件。在使用 OR 运算符时,当至少任何一个条件为 true 时,将假定完整条件为 true。例如,如果 condition1 或 condition2 是 true,则 [condition1] OR [condition2] 将为 true。
The OR operator is also used to combine multiple conditions in a PostgreSQL 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
OR 运算符与 WHERE 子句的基本语法如下 −
The basic syntax of OR operator with WHERE clause is as follows −
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] OR [condition2]...OR [conditionN]
您可以使用 OR 运算符组合 N 个条件。PostgreSQL 语句执行某个操作(无论是事务还是查询),OR 分隔的条件中只需任何一个为 TRUE。
You can combine N number of conditions using OR operator. For an action to be taken by the PostgreSQL statement, whether it be a transaction or query, only any ONE of the conditions separated by the OR must be TRUE.
Example
考虑 COMPANY 表,其中包含以下记录 −
Consider the COMPANY table, having the following records −
# select * from COMPANY;
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall| 45000
7 | James | 24 | Houston | 10000
(7 rows)
以下 SELECT 语句列出所有 AGE 大于或等于 25 OR 薪水大于或等于 65000.00 的记录 -
The 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 −
testdb=# SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000;
以上给出的 PostgreSQL 语句将产生以下结果 -
The above given PostgreSQL statement will produce the following result −
id | name | age | address | salary
----+-------+-----+------------+--------
1 | Paul | 32 | California | 20000
2 | Allen | 25 | Texas | 15000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
(4 rows)