GNU Info

Info Node: (mysql.info)Selecting rows

(mysql.info)Selecting rows


Next: Selecting columns Prev: Selecting all Up: Retrieving data
Enter node , (file) or (file)node

Selecting Particular Rows
.........................

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 now as 1989,
not 1998.

String comparisons are normally case insensitive, so you can specify the
name as `"bowser"', `"BOWSER"', etc.  The query result will be the same.

You can specify conditions on any column, not just `name'.  For example,
if you want to know which animals were born 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.  If you do that, it's a good idea to
use parentheses to indicate 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  |
     +-------+--------+---------+------+------------+-------+


automatically generated by info2www version 1.2.2.9