教程 > postgresql 教程 > 阅读:47

postgresql where 子句——迹忆客-ag捕鱼王app官网

在 postgresql 中,当我们需要根据指定条件从单张表或者多张表中查询数据时,就可以在 select 语句中添加 where 子句,从而过滤掉我们不需要数据。

where 子句不仅用在 select 语句中,它也用在 update、delete 语句等中,我们将在后续章节中进行介绍。

语法

以下是 select 语句中使用 where 子句从数据库中读取数据的通用语法:

select column1, column2, columnn
from table_name
where [condition1]

我们可以在 where 子句中使用比较运算符或逻辑运算符,例如 >, <, =, like, not 等等。

创建 company 表( ),数据内容如下:

jiyik_db=# 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 的数据:

jiyik_db=# 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 的数据:

jiyik_db=# 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(年龄) 字段不为空的记录:

jiyik_db=#  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 开头的的数据:

jiyik_db=# select * from company where name like 'pa%';

结果如下:

id | name | age |address    | salary
---- ------ ----- ----------- --------
  1 | paul |  32 | california|  20000

in

以下 select 语句列出了 age(年龄) 字段为 25 或 27 的数据:

jiyik_db=# 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 的数据:

jiyik_db=# 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 的数据:

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

子查询

以下的 select 语句使用了 sql 的子查询,子查询语句中读取 salary(薪资) 字段大于 65000 的数据,然后通过 exists 运算符判断它是否返回行,如果有返回行则读取所有的 age(年龄) 字段。

jiyik_db=# select age from company
        where exists (select age from company where salary > 65000);

结果如下:

 age
-----
  32
  25
  23
  25
  27
  22
  24
(7 rows)

以下的 select 语句同样使用了 sql 的子查询,子查询语句中读取 salary(薪资) 字段大于 65000 的 age(年龄) 字段数据,然后用 > 运算符查询大于该 age(年龄) 字段数据:

jiyik_db=# select * from company
        where age > (select age from company where salary > 65000);

结果如下:

 id | name | age | address    | salary
---- ------ ----- ------------ --------
  1 | paul |  32 | california |  20000

查看笔记

扫码一下
查看教程更方便
网站地图