当我们需要根据指定条件从单张表或者多张表中查询数据时,就可以在 SELECT 语句中添加 WHERE 子句,从而过滤掉我们不需要数据。
WHERE子句不仅可以用于SELECT语句中,同时也可以用于UPDATE,DELETE等语句中。

1.语法

  1. SELECT column1, column2, columnN
  2. FROM 表名称
  3. WHERE [条件]

WHERE子句中可以使用比较运算符或逻辑运算符;

和AND结合使用

找出 AGE(年龄) 字段大于等于 25,并且 SALARY(薪资) 字段大于等于 65000 的数据:

runoobdb=# 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 行记录)

和OR结合使用

找出 AGE(年龄) 字段大于等于 25,或者 SALARY(薪资) 字段大于等于 65000 的数据:

runoobdb=# 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 行记录)

和NOT NULL结合使用

在公司表中找出 AGE(年龄) 字段不为空的记录:

runoobdb=# 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 行记录)

LIKE

在 COMPANY 表中找出 NAME(名字) 字段中以 Pa 开头的数据:

runoobdb=# SELECT * FROM company WHERE name LIKE 'Pa%';
 id | name | age |                      address                       | salary
----+------+-----+----------------------------------------------------+--------
  1 | Paul |  32 | California                                         |  20000
(1 行记录)

在 COMPANY 表中找出 NAME(名字) 字段中以 y结尾的数据:

runoobdb=# SELECT * FROM company WHERE name LIKE '%y';
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  3 | Teddy |  23 | Norway                                             |  20000
(1 行记录)

IN

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

runoobdb=# 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 行记录)

注意:这个IN不是在25~27之间的意思,是指25和27的意思
比如下面列出年龄为,25,27,32的数据:

runoobdb=# SELECT * FROM company WHERE age IN (25,27,32);
 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 行记录)

NOT IN

找出 AGE(年龄) 字段不为 25 或 27 的数据:

runoobdb=# 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 行记录)

找出 AGE(年龄) 字段不为 25 或 27 或,32的数据:

runoobdb=# SELECT * FROM company WHERE age NOT IN(25,27,32);
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  3 | Teddy |  23 | Norway                                             |  20000
  6 | Kim   |  22 | South-Hall                                         |  45000
  7 | James |  24 | Houston                                            |  10000
(3 行记录)
--这里就没有了age=32的数据
--in不等于between

BETWEEN

列出AGE范围在25到32的数据:

runoobdb=# SELECT * FROM company WHERE age BETWEEN 25 AND 32;
 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 行记录)

子查询

查询薪资大于65000的人的年龄分布;
可以先查询出所有薪资大于65000的数据;然后将返回的数据给下一个语句处理,查询出相应的年龄;

runoobdb=# SELECT age FROM company
runoobdb-# WHERE EXISTS(SELECT AGE FROM company WHERE salary > 6500);
 age
-----
  32
  25
  23
  25
  27
  22
  24
(7 行记录)

查询出薪资大于65000的人,然后查询大于这些人年龄的数据;

runoobdb=# SELECT * FROM company WHERE age > (SELECT age FROM company WHERE salary > 65000);
 id | name | age |                      address                       | salary
----+------+-----+----------------------------------------------------+--------
  1 | Paul |  32 | California                                         |  20000
(1 行记录)