1.分类

  • 算术运算符
  • 比较运算符
  • 逻辑运算符
  • 按位运算符

    2.算数运算符

    集中常见的算数运算符,假设a=2,b=4;
运算符 描述 示例
+
-
*
/ 除(取整) b/a = 2
% 模(取余) b%a=0
^ 指数 a^b=16

示例

  1. runoobdb=# select 2+3;
  2. ?column?
  3. ----------
  4. 5
  5. (1 行记录)
  6. runoobdb=# select 2*3;
  7. ?column?
  8. ----------
  9. 6
  10. (1 行记录)
  11. runoobdb=# select 4/2;
  12. ?column?
  13. ----------
  14. 2
  15. (1 行记录)
  16. runoobdb=# select 12%5;
  17. ?column?
  18. ----------
  19. 2
  20. (1 行记录)
  21. runoobdb=# select 2^3;
  22. ?column?
  23. ----------
  24. 8
  25. (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 行记录)