1.分类
运算符 | 描述 | 示例 |
---|---|---|
+ | 加 | |
- | 减 | |
* | 乘 | |
/ | 除(取整) | b/a = 2 |
% | 模(取余) | b%a=0 |
^ | 指数 | a^b=16 |
示例
runoobdb=# select 2+3;
?column?
----------
5
(1 行记录)
runoobdb=# select 2*3;
?column?
----------
6
(1 行记录)
runoobdb=# select 4/2;
?column?
----------
2
(1 行记录)
runoobdb=# select 12%5;
?column?
----------
2
(1 行记录)
runoobdb=# select 2^3;
?column?
----------
8
(1 行记录)
3.比较运算符
假设变量a=1,变量b等于2;
运算符 | 描述 | 示例 |
---|---|---|
= | 等于 | (a=b)为false |
!= | 不等于 | (a!=b)为true |
<> | 不等于 | (a<>b)为true |
> | 大于 | (a>b)为false |
< | 小于 | (a<b)为true |
>= | 大于等于 | (a>=b)为false |
<= | 小于等于 | (a<=b)为true |
4.示例
创建company表,可由company.sql导入;
runoobdb=# 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 行记录)
读取 SALARY 字段大于 50000 的数据;
runoobdb=# SELECT * FROM company WHERE SALARY > 50000;
id | name | age | address | salary
----+-------+-----+----------------------------------------------------+--------
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
(2 行记录)
读取 SALARY 字段等于 20000 的数据:
runoobdb=# SELECT * FROM company WHERE SALARY = 20000;
id | name | age | address | salary
----+-------+-----+----------------------------------------------------+--------
1 | Paul | 32 | California | 20000
3 | Teddy | 23 | Norway | 20000
(2 行记录)
读取 SALARY 字段不等于 20000 的数据:
runoobdb=# 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 行记录)
runoobdb=# 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 行记录)
读取 SALARY 字段大于等于 65000 的数据:
runoobdb=# select * from company where salary > 65000;
id | name | age | address | salary
----+-------+-----+----------------------------------------------------+--------
5 | David | 27 | Texas | 85000
(1 行记录)
5.逻辑运算符
运算符 | 描述 |
---|---|
AND | 如果两个操作数都非零,则条件为真; PostgresSQL 中的 WHERE 语句可以用 AND 包含多个过滤条件。 |
NOT | 如果条件为真则逻辑非运算符将使其为假; PostgresSQL 有 NOT EXISTS, NOT BETWEEN, NOT IN 等运算符 |
OR | 如果两个操作数中有任意一个非零,则条件为真。 PostgresSQL 中的 WHERE 语句可以用 OR 包含多个过滤条件。 |
6.示例
读取 AGE 字段大于等于 25 且 SALARY 字段大于等于 6500 的数据:
runoobdb=# 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 行记录)
读取 AGE 字段大于等于 25 或 SALARY 字段大于 6500 的数据:
runoobdb=# 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
(7 行记录)
读取 SALARY 字段不为 NULL 的数据:
runoobdb=# 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
(7 行记录)