postgresql 运算符——迹忆客-ag捕鱼王app官网
运算符是一种告诉编译器执行特定的数学或逻辑操作的符号。
postgresql 运算符是一个保留关键字或字符,一般用在 where 语句中,作为过滤条件。
postgresql 中的常见运算符为
- 算术运算符
- 比较运算符
- 逻辑运算符
- 按位运算符
算术运算符
假设变量 a 为 2,变量 b 为 3,则:
运算符 | 描述 | 实例 |
---|---|---|
加 | a b 结果为 5 | |
- | 减 | a - b 结果为 -1 |
* | 乘 | a * b 结果为 6 |
/ | 除 | b / a 结果为 1 |
% | 模(取余) | b % a 结果为 1 |
^ | 指数 | a ^ b 结果为 8 |
/ | 平方根 | |
/ | ||
! | 阶乘 | 5 ! 结果为 120 |
!! | 阶乘 | (前缀操作符) !! 5 结果为 120 |
示例
jiyik_db=# select 2 3;
?column?
----------
5
(1 row)
jiyik_db=# select 2*3;
?column?
----------
6
(1 row)
jiyik_db=# select 10/5;
?column?
----------
2
(1 row)
jiyik_db=# select 12%5;
?column?
----------
2
(1 row)
jiyik_db=# select 2^3;
?column?
----------
8
(1 row)
jiyik_db=# select |/ 25.0;
?column?
----------
5
(1 row)
jiyik_db=# select ||/ 27.0;
?column?
----------
3
(1 row)
jiyik_db=# select 5 !;
?column?
----------
120
(1 row)
jiyik_db=# select !!5;
?column?
----------
120
(1 row)
比较运算符
假设变量 a 为 10,变量 b 为 20,则:
运算符 | 描述 | 实例 |
---|---|---|
= | 等于 | (a = b) 为 false。 |
!= | 不等于 | (a != b) 为 true。 |
<> | 不等于 | (a <> b) 为 true。 |
> | 大于 | (a > b) 为 false。 |
< | 小于 | (a < b) 为 true。 |
>= | 大于等于 | (a >= b) 为 false。 |
<= | 小于等于 | (a <= b) 为 true。 |
示例
创建 company 表( ),数据内容如下:
jiyik_db=# 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)
读取 salary 字段大于 50000 的数据:
jiyik_db=# select * from company where salary > 50000;
结果如下:
id | name | age |address | salary
---- ------- ----- ----------- --------
4 | mark | 25 | rich-mond | 65000
5 | david | 27 | texas | 85000
(2 rows)
读取 salary 字段等于 20000 的数据:
jiyik_db=# select * from company where salary = 20000;
id | name | age | address | salary
---- ------- ----- ------------- --------
1 | paul | 32 | california | 20000
3 | teddy | 23 | norway | 20000
(2 rows)
读取 salary 字段不等于 20000 的数据:
jiyik_db=# select * from company where salary != 20000;
结果如下:
id | name | age | address | salary
---- ------- ----- ------------- --------
2 | allen | 25 | texas | 15000
4 | mark | 25 | rich-mond | 65000
5 | david | 27 | texas | 85000
6 | kim | 22 | south-hall | 45000
7 | james | 24 | houston | 10000
(5 rows)
读取 salary 字段不等于 20000 的数据的另一种方式:
jiyik_db=# select * from company where salary <> 20000;
结果如下
id | name | age | address | salary
---- ------- ----- ------------ --------
2 | allen | 25 | texas | 15000
4 | mark | 25 | rich-mond | 65000
5 | david | 27 | texas | 85000
6 | kim | 22 | south-hall | 45000
7 | james | 24 | houston | 10000
(5 rows)
读取 salary 字段大于等于 65000 的数据:
jiyik_db=# select * from company where salary >= 65000;
结果如下:
id | name | age | address | salary
---- ------- ----- ----------- --------
4 | mark | 25 | rich-mond | 65000
5 | david | 27 | texas | 85000
(2 rows)
逻辑运算符
postgresql 逻辑运算符有以下几种:
序号 | 运算符 | 描述 |
---|---|---|
1 | and | 逻辑与运算符。如果两个操作数都非零,则条件为真。postgressql 中的 where 语句可以用 and 包含多个过滤条件。 |
2 | not | 逻辑非运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。postgressql 有 not exists, not between, not in 等运算符。 |
3 | or | 逻辑或运算符。如果两个操作数中有任意一个非零,则条件为真。postgressql 中的 where 语句可以用 or 包含多个过滤条件。 |
sql 使用三值的逻辑系统,包括 true、false 和 null,null 表示"未知"。
示例
创建 company 表,数据内容如下:
jiyik_db=# 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)
读取 age 字段大于等于 25 且 salary 字段大于等于 6500 的数据:
jiyik_db=# select * from company where age >= 25 and salary >= 6500;
结果如下:
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)
读取 age 字段大于等于 25 或 salary 字段大于 6500 的数据:
jiyik_db=# select * from company where age >= 25 or salary >= 6500;
结果如下:
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
8 | paul | 24 | houston | 20000
9 | james | 44 | norway | 5000
10 | james | 45 | texas | 5000
(10 rows)
读取 salary 字段不为 null 的数据:
jiyik_db=# select * from company where salary is not null;
结果如下:
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
8 | paul | 24 | houston | 20000
9 | james | 44 | norway | 5000
10 | james | 45 | texas | 5000
(10 rows)
位运算符
位运算符作用于位,并逐位执行操作。&、 | 和 ^ 的真值表如下所示:
p| q| p & q| p | q -- | -- | --| -- 0| 0| 0| 0 0| 1| 0| 1 1| 1| 1| 1 1| 0| 0| 1
假设如果 a = 60,且 b = 13,现在以二进制格式表示,它们如下所示:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
postgresql 支持的按位运算符如下表所示
运算符 | 描述 | 实例 |
---|---|---|
& | 按位与操作,按二进制位进行"与"运算。 | (a & b) 将得出 12,即 0000 1100 |
按位或运算符,按二进制位进行"或"运算。 | ||
~ | 取反运算符,按二进制位进行"取反"运算 | (~a ) 将得出 -61,它是 1100 0011 由于带符号的二进制数,以 2 的补码形式表示。 |
<< | 二进制左移运算符。将一个运算对象的各二进制位全部左移若干位(左边的二进制位丢弃,右边补0)。 | a << 2 将得出 240,即 1111 0000 |
>> | 二进制右移运算符。将一个数的各二进制位全部右移若干位,正数左补0,负数左补1,右边丢弃。 | a >> 2 将得出 15,即 0000 1111 |
示例
jiyik_db=# select 60 | 13;
?column?
----------
61
(1 row)
jiyik_db=# select 60 & 13;
?column?
----------
12
(1 row)
jiyik_db=# select (~60);
?column?
----------
-61
(1 row)
jiyik_db=# select (60 << 2);
?column?
----------
240
(1 row)
jiyik_db=# select (60 >> 2);
?column?
----------
15
(1 row)
jiyik_db=# select 60 # 13;
?column?
----------
49
(1 row)