PostgreSQL - 比较运算符



考虑如下记录的表 COMPANY

testdb=# 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)

以下是一些简单的例子,展示了 PostgreSQL 比较运算符的用法。

这里我们使用了WHERE子句,这将在单独的章节中解释,但现在您可以理解 WHERE 子句用于在 SELECT 语句中添加条件语句。

以下 SELECT 语句列出所有薪水大于 50,000.00 的记录:

testdb=# SELECT * FROM COMPANY WHERE SALARY > 50000;

上述 PostgreSQL 语句将产生以下结果:

 id | name  | age |address    | salary
----+-------+-----+-----------+--------
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
(2 rows)

以下 SELECT 语句列出所有薪水等于 20,000.00 的记录:

testdb=#  SELECT * FROM COMPANY WHERE SALARY = 20000;

上述 PostgreSQL 语句将产生以下结果:

  id | name  | age |  address    | salary
 ----+-------+-----+-------------+--------
   1 | Paul  |  32 | California  |  20000
   3 | Teddy |  23 | Norway      |  20000
(2 rows)

以下 SELECT 语句列出所有薪水不等于 20,000.00 的记录:

testdb=#  SELECT * FROM COMPANY WHERE SALARY != 20000;

上述 PostgreSQL 语句将产生以下结果:

 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)

以下 SELECT 语句列出所有薪水不等于 20,000.00 的记录:

testdb=# SELECT * FROM COMPANY WHERE SALARY <> 20000;

上述 PostgreSQL 语句将产生以下结果:

 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)

以下 SELECT 语句列出所有薪水大于或等于 65,000.00 的记录:

testdb=# SELECT * FROM COMPANY WHERE SALARY >= 65000;

上述 PostgreSQL 语句将产生以下结果:

 id | name  | age |  address  | salary
----+-------+-----+-----------+--------
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
(2 rows)
postgresql_operators.htm
广告