教程 > postgresql 教程 > 阅读:47

postgresql and & or 运算符——迹忆客-ag捕鱼王app官网

在 postgresql 中,and 和 or 也叫连接运算符,在查询数据时用于缩小查询范围,我们可以用 and 或者 or 指定一个或多个查询条件。


and 运算符

and 运算符表示一个或者多个条件必须同时成立。

在 where 子句中,and 的使用语法如下:

select column1, column2, columnn
from table_name
where [condition1] and [condition2]...and [conditionn];

示例

创建 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)

以下示例读取 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 运算符

or 运算符表示多个条件中只需满足其中任意一个即可。

在 where 子句中,or 的使用语法如下:

select column1, column2, columnn
from table_name
where [condition1] or [condition2]...or [conditionn]

创建 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)

以下实例读取 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)

查看笔记

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