As shown in the preceding section, it is easy to retrieve an
entire table. Just omit the WHERE clause
from the SELECT statement. 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:
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.
String comparisons normally are case-insensitive, so you can
specify the name as 'bowser',
'BOWSER', and so forth. The query result is
the same.
You can specify conditions on any column, not just
name. For example, if you want to know
which animals were born during or after 1998, test the
birth column:
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. There is also an OR operator:
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. If 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 | +-------+--------+---------+------+------------+-------+

User Comments
If you want to know which animals were cat OR dog AND her owner was Harold.
Following this instruction below :
mysql>SELECT * FROM pet WHERE (species = 'dog' OR species = 'cat') AND owner = 'harold';
Hope it helps......
Xia Ren comes from People's Republic of China.
Chat Messenger:
"mysql> DELETE FROM `messenger` WHERE time_row < (UNIX_TIMESTAMP() - 1800)"
time_row : integer
Delete all messages down 30 seconds
It should be noted that you can't do something like the following:
select * from pet where (species = 'dog' and species = 'cat');
Any given attribute can only have one value (this is an SQL property called "atomicity"). If you translated the above SQL statement into English this makes more sense: "Show me all pets who are dogs and cats". A pet can't be both a cat and a dog at the same time! A better question is "Show me all pets who are dogs or cats":
select * from pet where (species = 'dog' or species = 'cat');
Here's an example of how the placement of () can make a difference, compared to taking the default precedence order of AND and OR -
5 rows in set (0.00 sec)mysql> select * from pet where species='cat' and sex='f' or owner='gwen';
mysql> select * from pet where species='cat' and (sex='f' or owner='gwen');
3 rows in set (0.00 sec)
mysql> select * from pet where (species='cat' and sex='f') or owner='gwen';
5 rows in set (0.01 sec)
mysql>
Add your own comment.