The world's most popular open source database
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
SELECT * FROM pet WHERE death;
yields:
Bowser Diane dog m 1989-08-31
but,
SELECT * FROM pet WHERE sex;
yields nothing.
That is probably because is the only one to have a value in death field. As for the 'sex' all fields have valid content.
This is an assumption.
Just a little more info: if you execute
SELECT * FROM pet WHERE sex;
you will get no output - but, you also get 8 warnings. You can toggle the display of warnings with the warning function:
warning; SELECT * FROM pet WHERE sex;
If you do that, then you get 8 of this warning:
Warning (Code 1292): Truncated incorrect INTEGER value: 'f'
Apparently, there's something MySQL doesn't like about the variable type the example is using... I guess.
A little more on this syntax that doesn't include an =...
1 row in set (0.00 sec)mysql> SELECT * FROM pet WHERE name;
Empty set (0.00 sec)
mysql> SELECT * FROM pet WHERE sex;
Empty set, 8 warnings (0.00 sec)
mysql> SELECT * FROM pet WHERE death;
Most likely it is because the death field (and the birth field as well) are of type DATE and MySQL does an internal conversion to a numerical value. Therefore there is a true/false value that can be determined. Anything non-null and non-zero is true and therefore the WHERE clause passes.
As a test, if you UPDATE the death date on a pet to '0000-00-00', the UPDATE succeeds and the field is no longer NULL. But the record still does not show up with the original query 'SELECT * FROM pet WHERE death;". MySQL is converting the string '0000-00-00' internally to the number zero.
Add your own comment.