PostgreSQL WHERE 子句
在 PostgreSQL 中,当我们需要根据指定条件从单张表或者多张表中查询数据时,就可以在 SELECT 语句中添加 WHERE 子句,从而过滤掉我们不需要数据。
WHERE 子句不仅可以用于 SELECT 语句中,同时也可以用于 UPDATE,DELETE 等等语句中。
语法
以下是 SELECT 语句中使用 WHERE 子句从数据库中读取数据的通用语法:
SELECT column1, column2, columnN FROM table_name WHERE [condition1]
我们可以在 WHERE 子句中使用比较运算符或逻辑运算符,例如 >, <, =, LIKE, NOT 等等。
创建 COMPANY 表(下载 COMPANY SQL 文件 ),数据内容如下:
NowJavadb# 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)以下几个实例我们使用逻辑运算符来读取表中的数据。
AND
找出 AGE(年龄) 字段大于等于 25,并且 SALARY(薪资) 字段大于等于 65000 的数据:
NowJavadb=# SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000; id | name | age | address | salary ----+-------+-----+------------+-------- 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 (2 rows)
OR
找出 AGE(年龄) 字段大于等于 25,或者 SALARY(薪资) 字段大于等于 65000 的数据:
NowJavadb=# SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000; 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)
NOT NULL
在公司表中找出 AGE(年龄) 字段不为空的记录:
NowJavadb=# SELECT * FROM COMPANY WHERE AGE 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 rows)
LIKE
在 COMPANY 表中找出 NAME(名字) 字段中以 Pa 开头的的数据:
NowJavadb=# SELECT * FROM COMPANY WHERE NAME LIKE 'Pa%'; id | name | age |address | salary ----+------+-----+-----------+-------- 1 | Paul | 32 | California| 20000
IN
以下 SELECT 语句列出了 AGE(年龄) 字段为 25 或 27 的数据:
NowJavadb=# SELECT * FROM COMPANY WHERE AGE IN ( 25, 27 ); id | name | age | address | salary ----+-------+-----+------------+-------- 2 | Allen | 25 | Texas | 15000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 (3 rows)
NOT IN
以下 SELECT 语句列出了 AGE(年龄) 字段不为 25 或 27 的数据:
NowJavadb=# SELECT * FROM COMPANY WHERE AGE NOT IN ( 25, 27 ); id | name | age | address | salary ----+-------+-----+------------+-------- 1 | Paul | 32 | California | 20000 3 | Teddy | 23 | Norway | 20000 6 | Kim | 22 | South-Hall | 45000 7 | James | 24 | Houston | 10000 (4 rows)
BETWEEN
以下 SELECT 语句列出了 AGE(年龄) 字段在 25 到 27 的数据:
NowJavadb=# SELECT * FROM COMPANY WHERE AGE BETWEEN 25 AND 27; id | name | age | address | salary ----+-------+-----+------------+-------- 2 | Allen | 25 | Texas | 15000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 (3 rows)