从表中检索信息

SELECT语句用于从表中提取信息,语句的一般形式是:

SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy;

what_to_select表示你想要看到的内容,这可以是列列表,或*表示“所有列”。which_table表示要从中检索数据的表,WHERE子句是可选的,如果存在,则conditions_to_satisfy指定行必须满足的一个或多个条件才有资格进行检索。

查询所有数据

最简单的SELECT形式从表中检索所有内容:

mysql> SELECT * FROM pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    | NULL | 1997-12-09 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
| Puffball | Diane  | hamster | f    | 1999-03-30 | NULL       |
+----------+--------+---------+------+------------+------------+

如果要查看整个表,例如在刚刚加载初始数据集之后,这种形式的SELECT非常有用。例如,你可能会认为Bowser的出生日期似乎不太合适,咨询你原来的资料,你会发现正确的出生年应该是1989年,而不是1979年。

至少有两种方法可以解决这个问题:

  • 编辑文件pet.txt以更正错误,然后使用DELETELOAD DATA清空表并重新加载它:

    mysql> DELETE FROM pet;
    mysql> LOAD DATA LOCAL INFILE 'pet.txt' INTO TABLE pet;

    但是,如果这样做,你还必须重新输入Puffball的记录。

  • 使用UPDATE语句仅修复错误记录:

    mysql> UPDATE pet SET birth = '1989-08-31' WHERE name = 'Bowser';

    UPDATE仅更改有问题的记录,不需要你重新加载表。

查询特定行

如上一节所示,可以轻松检索整个表,只需省略SELECT语句中的WHERE子句即可,但通常你不希望看到整个表,特别是当它变大时。相反,你通常对回答特定问题更感兴趣,在这种情况下,你可以对所需信息指定一些约束,让我们看看一些选择查询,它们会回答关于你的宠物的问题。

你只能从表中选择特定行,例如,如果你想验证你对Bowser出生日期所做的更改,请选择Bowser的记录,如下所示:

mysql> SELECT * FROM pet WHERE name = 'Bowser';
+--------+-------+---------+------+------------+------------+
| name   | owner | species | sex  | birth      | death      |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+

输出确认年份被正确记录为1989年,而不是1979年。

字符串比较通常不区分大小写,因此你可以将name指定为'bowser''BOWSER'等等,查询结果是一样的。

你可以在任何列上指定条件,而不仅仅是name,例如,如果你想知道1998年或之后出生的动物,请测试birth列:

mysql> SELECT * FROM pet WHERE birth >= '1998-1-1';
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Chirpy   | Gwen  | bird    | f    | 1998-09-11 | NULL  |
| Puffball | Diane | hamster | f    | 1999-03-30 | NULL  |
+----------+-------+---------+------+------------+-------+

例如,你可以结合条件来定位雌性狗:

mysql> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

前面的查询使用AND逻辑运算符,还有一个OR运算符:

mysql> SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Chirpy   | Gwen  | bird    | f    | 1998-09-11 | NULL  |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL  |
| Slim     | Benny | snake   | m    | 1996-04-29 | NULL  |
+----------+-------+---------+------+------------+-------+

ANDOR可以混合,但AND的优先级高于OR,如果你同时使用这两个运算符,最好使用括号明确指出条件应如何分组:

mysql> SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm')
       OR (species = 'dog' AND sex = 'f');
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

查询特定列

如果你不想查看表中的整行,只需命名你感兴趣的列,使用逗号分隔。例如,如果你想知道你的动物何时出生,请选择namebirth列:

mysql> SELECT name, birth FROM pet;
+----------+------------+
| name     | birth      |
+----------+------------+
| Fluffy   | 1993-02-04 |
| Claws    | 1994-03-17 |
| Buffy    | 1989-05-13 |
| Fang     | 1990-08-27 |
| Bowser   | 1989-08-31 |
| Chirpy   | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim     | 1996-04-29 |
| Puffball | 1999-03-30 |
+----------+------------+

要找出谁拥有宠物,请使用此查询:

mysql> SELECT owner FROM pet;
+--------+
| owner  |
+--------+
| Harold |
| Gwen   |
| Harold |
| Benny  |
| Diane  |
| Gwen   |
| Gwen   |
| Benny  |
| Diane  |
+--------+

请注意,查询只是从每条记录中检索owner列,其中一些列出现不止一次,要最小化输出,请通过添加关键字DISTINCT检索每个唯一的输出记录一次:

mysql> SELECT DISTINCT owner FROM pet;
+--------+
| owner  |
+--------+
| Benny  |
| Diane  |
| Gwen   |
| Harold |
+--------+

你可以使用WHERE子句将行选择与列选择组合在一起,例如,要仅获取狗和猫的出生日期,请使用以下查询:

mysql> SELECT name, species, birth FROM pet
       WHERE species = 'dog' OR species = 'cat';
+--------+---------+------------+
| name   | species | birth      |
+--------+---------+------------+
| Fluffy | cat     | 1993-02-04 |
| Claws  | cat     | 1994-03-17 |
| Buffy  | dog     | 1989-05-13 |
| Fang   | dog     | 1990-08-27 |
| Bowser | dog     | 1989-08-31 |
+--------+---------+------------+

上一篇:创建和使用数据库

博弈
2.5k 声望1.5k 粉丝

态度决定一切