Table of contents
non-symbolic operator
arithmetic operator
Suppose variable a is 2 and variable b is 3, then:
operator | describe | example |
---|---|---|
+ | add | a + b results in 5 |
- | reduce | a - b results in -1 |
* | take | a * b results in 6 |
/ | remove | b / a results in 1 |
% | Modulo (remainder) | b % a results in 1 |
^ | index | a ^ b results in 8 |
|/ | square root | |/ 25.0 results in 5 |
||/ | cube root | ||/ 27.0 results in 3 |
! | factorial | 5 ! The result is 120 |
!! | factorial (prefix operator) | !! 5 results in 120 |
comparison operator
Suppose variable a is 10 and variable b is 20, then:
operator | describe | example |
---|---|---|
= | equal | (a = b) is false. |
!= | not equal to | (a != b) is true. |
<> | not equal to | (a <> b) 为 true。 |
> | more than the | (a > b) is false. |
< | less than | (a < b) is true. |
>= | greater or equal to | (a >= b) is false. |
<= | less than or equal to | (a <= b) is true. |
Logical Operators
PostgreSQL logical operators are as follows:
serial number | operator & description |
---|---|
1 | AND Logical AND operator. The condition becomes true if both operands are non-zero. A WHERE statement in PostgresSQL can contain multiple filter conditions using AND. |
2 | NOT Logical NOT operator. Used to invert the logical state of an operand. The logical NOT operator will make a condition false if it is true. PostgresSQL has NOT EXISTS, NOT BETWEEN, NOT IN operators. |
3 | OR Logical OR operator. The condition becomes true if either of the two operands is non-zero. A WHERE statement in PostgresSQL can contain multiple filter conditions using OR. |
SQL uses a logical system of three values, including true, false, and null, where null means "unknown".
a |
b |
a AND b |
a OR b |
---|---|---|---|
TRUE | TRUE | TRUE | TRUE |
TRUE | FALSE | FALSE | TRUE |
TRUE | NULL | NULL | TRUE |
FALSE | FALSE | FALSE | FALSE |
FALSE | NULL | FALSE | NULL |
NULL | NULL | NULL | NULL |
a |
NOT a |
---|---|
TRUE | FALSE |
FALSE | TRUE |
NULL | NULL |
bitwise operator
Bitwise operators operate on bits and perform operations bit by bit. The truth tables for &, |, and ^ are as follows:
p | q | p & q | p | q |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 |
1 | 1 | 1 | 1 |
1 | 0 | 0 | 1 |
The following table shows the bitwise operators supported by PostgreSQL. Assuming variable A has a value of 60 and variable B has a value of 13, then:
operator | describe | example |
---|---|---|
& | Bitwise AND operation, "AND" operation is performed by binary digits. Operation rules: 0&0=0; 0&1=0; 1&0=0; 1&1=1; |
(A & B) will get 12 which is 0000 1100 |
| | The bitwise OR operator performs an "or" operation on a binary basis. Operation rules: 0|0=0; 0|1=1; 1|0=1; 1|1=1; |
(A | B) will get 61 which is 0011 1101 |
# | XOR operator, perform "XOR" operation by binary bit. Operation rules: 0#0=0; 0#1=1; 1#0=1; 1#1=0; |
(A # B) will get 49 which is 0011 0001 |
~ | Inversion operator, perform "inversion" operation according to binary bits. Operation rules: ~1=0; ~0=1; |
(~A ) will get -61, which is 1100 0011, the two's complement form of a signed binary number. |
<< | Binary left shift operator. Shift all binary bits of an operand to the left by several bits (the binary bits on the left are discarded, and 0 is added to the right). | A << 2 will get 240 which is 1111 0000 |
>> | Binary right shift operator. All the binary digits of a number are shifted to the right by several bits, positive numbers are left filled with 0, negative numbers are left filled with 1, and the right side is discarded. |