Oracle 5.0 Reference Manual page 223

Table of Contents

Advertisement

+----------+-------+---------+------+------------+-------+
You can combine conditions, for example, to locate female dogs:
mysql>
SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+-------+--------+---------+------+------------+-------+
| name
| owner
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog
+-------+--------+---------+------+------------+-------+
The preceding query uses the
mysql>
SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';
+----------+-------+---------+------+------------+-------+
| name
| owner | species | sex
+----------+-------+---------+------+------------+-------+
| Chirpy
| Gwen
| Whistler | Gwen
| Slim
| Benny | snake
+----------+-------+---------+------+------------+-------+
[880]
and
AND
[881]. If you use both operators, it is a good idea to use parentheses to indicate explicitly how
OR
conditions should be grouped:
mysql>
SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm')
->
OR (species = 'dog' AND sex = 'f');
+-------+--------+---------+------+------------+-------+
| name
| owner
+-------+--------+---------+------+------------+-------+
| Claws | Gwen
| Buffy | Harold | dog
+-------+--------+---------+------+------------+-------+
3.3.4.3. Selecting Particular Columns
If you do not want to see entire rows from your table, just name the columns in which you are
interested, separated by commas. For example, if you want to know when your animals were born,
select the
name
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 |
+----------+------------+
To find out who owns pets, use this query:
mysql>
SELECT owner FROM pet;
+--------+
| owner
|
+--------+
| Harold |
| Gwen
|
| Harold |
| Benny
|
| Diane
|
| Gwen
|
| Gwen
|
Retrieving Information from a Table
| species | sex
| birth
| f
| 1989-05-13 | NULL
[880]
AND
| bird
| f
| bird
| NULL | 1997-12-09 | NULL
| m
[881]
may be intermixed, although
OR
| species | sex
| birth
| cat
| m
| 1994-03-17 | NULL
| f
| 1989-05-13 | NULL
and
columns:
birth
|
| death |
|
logical operator. There is also an
| birth
| death |
| 1998-09-11 | NULL
|
|
| 1996-04-29 | NULL
|
[880]
AND
| death |
|
|
203
[881]
operator:
OR
has higher precedence than

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents