Postgresql 简明教程
PostgreSQL - Operators
What is an Operator in PostgreSQL?
运算符是保留字或主要用于 PostgreSQL 语句的 WHERE 子句中的字符,用于执行运算(例如,比较和数学运算)。
运算符用于指定 PostgreSQL 语句中的条件,并作为语句中多个条件的合取。
-
Arithmetic operators
-
Comparison operators
-
Logical operators
-
Bitwise operators
PostgreSQL Arithmetic Operators
假设变量 a 存储 2,变量 b 存储 3,那么 −
Operator |
Description |
Example |
+ |
加法 - 添加运算符两侧的值 |
a + b 将得到 5 |
- |
减法 - 从左操作数中减去右操作数 |
a - b 将得到 -1 |
* |
乘法 - 乘以运算符两侧的值 |
a * b 将得到 6 |
/ |
除法 - 将左操作数除以右操作数 |
b/a 将返回 1 |
% |
取模 - 将左操作数除以右操作数并返回余数 |
b % a 将返回 1 |
^ |
求幂 - 返回右操作数的指数值 |
a ^ b 将返回 8 |
/ |
square root |
|
/ 25.0 将返回 5 |
||
/ |
Cube root |
|
/ 27.0 将返回 3 |
||
! |
factorial |
5! 将返回 120 |
!! |
factorial (prefix operator) |
!! 5 将返回 120 |
PostgreSQL Comparison Operators
假设变量 a 等于 10,变量 b 等于 20,则 −
Operator |
Description |
Example |
= |
检查两个操作数的值是否相等,如果相等,则条件变为真。 |
(a = b) 不为真。 |
!= |
检查两个操作数的值是否相等,如果值不相等,则条件变为真。 |
(a != b) 为 true。 |
<> |
检查两个操作数的值是否相等,如果值不相等,则条件变为真。 |
(a <> b) 为真。 |
> |
检查左操作数的值是否大于右操作数的值,如果大于,则条件变为真。 |
(a > b) 为 false。 |
< |
检查左操作数的值是否小于右操作数的值,如果小于,则条件变为真。 |
(a < b) 为 true。 |
>= |
检查左操作数的值是否大于或等于右操作数的值,如果大于或等于,则条件变为真。 |
(a >= b) 为 false。 |
⇐ |
检查左操作数的值是否小于或等于右操作数的值,如果小于或等于,则条件变为真。 |
(a ⇐ b) 为 true。 |
PostgreSQL Logical Operators
以下是 PostgresSQL 中所有可用的逻辑运算符列表。
S. No. |
Operator & Description |
1 |
AND AND 运算符允许在 PostgresSQL 语句的 WHERE 子句中存在多个条件。 |
2 |
NOT NOT 运算符反转与其一起使用的逻辑运算符的含义。例如:NOT EXISTS、NOT BETWEEN、NOT IN 等 This is negate operator 。 |
3 |
OR OR 运算符用于将多个条件组合成一个 PostgresSQL 语句的 WHERE 子句。 |
PostgreSQL Bit String Operators
按位运算符处理位,并执行逐位运算。& 和 | 的真值表如下 −
p |
q |
p & |
p |
q |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
0 |
0 |
假设如果 A = 60;并且 B = 13;现在以二进制格式,它们将如下所示 −
A = 0011 1100
B = 0000 1101
A&B = 0000 1100 A|B = 0011 1101 ~A = 1100 0011 link:../postgresql/postgresql_bitwise-operators.html[Show Examples] The Bitwise operators supported by PostgreSQL are listed in the following table − [%autowidth] |=== |Operator|Description|Example |&|Binary AND Operator copies a bit to the result if it exists in both operands.|(A & B) will give 12 which is 0000 1100 |||Binary OR Operator copies a bit if it exists in either operand.|(A | B) will give 61 which is 0011 1101 |~|Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.|(~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. |<<|Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.|A << 2 will give 240 which is 1111 0000 |>>|Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.|A >> 2 will give 15 which is 0000 1111 |#|bitwise XOR.|A # B will give 49 which is 00110001 |===