3.3.4.2 Selecting Particular Rows选择特定行

As shown in the preceding section, it is easy to retrieve an entire table. 如前一节所示,检索整个表很容易。Just omit the WHERE clause from the SELECT statement. 只需从SELECT语句中省略WHERE子句。But typically you don't want to see the entire table, particularly when it becomes large. 但通常您不希望看到整个表,尤其是当它变大时。Instead, you're usually more interested in answering a particular question, in which case you specify some constraints on the information you want. 相反,你通常更感兴趣的是回答一个特定的问题,在这种情况下,你可以对你想要的信息指定一些约束条件。Let's look at some selection queries in terms of questions about your pets that they answer.让我们看看一些选择查询,它们回答的关于宠物的问题。

You can select only particular rows from your table. 您只能从表中选择特定行。For example, if you want to verify the change that you made to Bowser's birth date, select Bowser's record like this:例如,如果要验证对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 |
+--------+-------+---------+------+------------+------------+

The output confirms that the year is correctly recorded as 1989, not 1979.这一产出证实,这一年正确地记录为1989年,而不是1979年。

String comparisons normally are case-insensitive, so you can specify the name as 'bowser', 'BOWSER', and so forth. 字符串比较通常不区分大小写,因此可以将名称指定为'bowser''BOWSER',等等。The query result is the same.查询结果相同。

You can specify conditions on any column, not just name. 您可以在任何列上指定条件,而不仅仅是nameFor example, if you want to know which animals were born during or after 1998, test the birth column:例如,如果要知道哪些动物是在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  |
+----------+-------+---------+------+------------+-------+

You can combine conditions, for example, to locate female dogs:例如,您可以组合条件来定位母狗:

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

The preceding query uses the AND logical operator. 前面的查询使用AND逻辑运算符。There is also an OR operator:还有一个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  |
+----------+-------+---------+------+------------+-------+

AND and OR may be intermixed, although AND has higher precedence than OR. ANDOR可以混合,但AND的优先级高于ORIf you use both operators, it is a good idea to use parentheses to indicate explicitly how conditions should be grouped:如果同时使用这两个运算符,最好使用括号明确指示条件应如何分组:

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  |
+-------+--------+---------+------+------------+-------+