Databases Reference
In-Depth Information
value, or a string that is a prefix of another. Almost all our examples in this and later
chapters contain WHERE clauses, and you'll become very familiar with them.
The simplest WHERE clause is one that exactly matches a value. Consider an example
where we want to find out the details of the artist with the name “New Order.” Here's
what you type:
mysql> SELECT * FROM artist WHERE artist_name = "New Order";
+-----------+-------------+
| artist_id | artist_name |
+-----------+-------------+
| 1 | New Order |
+-----------+-------------+
1 row in set (0.00 sec)
MySQL returns all rows that match our search criteria—in this case, just the one row
and all its columns. From this, you can see that the artist “New Order” has an
artist_id of 1.
Let's try another exact-match example. Suppose you want to find out the name of the
artist with an artist_id value of 4. You type:
mysql> SELECT artist_name FROM artist WHERE artist_id = 4;
+--------------------+
| artist_name |
+--------------------+
| The Rolling Stones |
+--------------------+
1 row in set (0.00 sec)
In this example, we've chosen both a column and a row: we've included the column
name artist_name after the SELECT keyword, as well as WHERE artist_id = 4 .
If a value matches more than one row, the results will contain all matches. Suppose we
ask for the names of all tracks with a track_id of 13; this retrieves the thirteenth song
on every album that has at least that many songs. You type in:
mysql> SELECT track_name FROM track WHERE track_id = 13;
+------------------------------------------+
| track_name |
+------------------------------------------+
| Every Little Counts |
| Everyone Everywhere |
| Turn My Way [Olympia, Liverpool 18/7/01] |
| Let It Loose |
+------------------------------------------+
4 rows in set (0.02 sec)
The results show the names of the thirteenth track of different albums, so there must
be 4 albums that contain at least 13 tracks If we could join the information we get from
the track table with information we get from the album table, we could display the
names of these albums. We'll see how to perform this type of query later in “Joining
Two Tables.”
 
Search WWH ::




Custom Search