考虑一下表 COMPANY 有以下记录: gitbook.net
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 | gitbook.net | 24 | Houston2 | 10000
(7 rows)
www.gitbook.net
下面是简单的例子,显示使用PostgreSQL比较操作符: gitbook.net
在这里,我们已经使用WHERE子句将在一个单独的章节解释,但现在可以明白,WHERE子句用于把一个条件语句与SELECT语句一起。
下面的SELECT语句列表的工资大于50,000.00下来的所有记录:
testdb=# SELECT * FROM COMPANY WHERE SALARY > 50000; gitbook.net
以上PostgreSQL的表会产生以下结果: gitbook.net
id | name | age |address | salary ----+-------+-----+-----------+-------- 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 (2 rows) gitbook.net
下面的SELECT语句了所有的记录,工资等于20,000.00列表:
www.gitbook.net
testdb=# SELECT * FROM COMPANY WHERE SALARY = 20000; gitbook.net
以上PostgreSQL的表会产生以下结果: gitbook.net
id | name | age | address | salary ----+-------+-----+-------------+-------- 1 | Paul | 32 | California | 20000 3 | Teddy | 23 | Norway | 20000 (2 rows)gitbook.net
下面的SELECT语句了所有的记录,工资不等于20,000.00列表: www.gitbook.net
testdb=# SELECT * FROM COMPANY WHERE SALARY != 20000; www.gitbook.net
以上PostgreSQL表会产生以下结果:
www.gitbook.net
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)www.gitbook.net
下面的SELECT语句了所有的记录,工资不等于20,000.00列表:
gitbook.net
testdb=# SELECT * FROM COMPANY WHERE SALARY <> 20000; www.gitbook.net
以上PostgreSQL的表会产生以下结果:
gitbook.net
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)www.gitbook.net
下面的SELECT语句列表的工资大于或等于65,000.00下来的所有记录: gitbook.net
testdb=# SELECT * FROM COMPANY WHERE SALARY >= 65000;www.gitbook.net
以上PostgreSQL表会产生以下结果: gitbook.net
id | name | age | address | salary ----+-------+-----+-----------+-------- 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 (2 rows)www.gitbook.net