SELECT Queries and Perl - More Complicated SELECTs (
Page 3 of 4 )
Sometimes we want to select information from our database to satisfy criteria that are a bit more complicated than what we have seen so far. For instance, if we use our database for what it was really created for, finding out what instrument a particular musician plays, it is going to be more complex.
As an example, if we wanted to find out what instruments Lenny Kravitz plays, we would have to first find out what his
player_id
is by querying the
musicians
table; then using that
player_id
, select the
inst_id
s out of
what_they_play
; then for each of those
inst_id
s, we can then get the
instrument
name out of the
instruments
table.
First, get Lenny Kravitz’s
player_id
:
mysql> SELECT player_id FROM musicians WHERE name = "Lenny Kravitz";
+-----------+
| player_id |
+-----------+
| 5 |
+-----------+
1 row in set (0.00 sec)
Now, using his
player_id
of 5, grab the
inst_id
s out of
what_they_play
:
mysql> SELECT inst_id FROM what_they_play WHERE player_id = 5;
+---------+
| inst_id |
+---------+
| 11 |
| 14 |
+---------+
2 rows in set (0.00 sec)
And the last step is, for each of the
inst_id
s, 11 and 14, query the
instruments
table for the
instrument
:
mysql> SELECT instrument FROM instruments WHERE inst_id = 11;
+------------+
| instrument |
+------------+
| guitar |
+------------+
1 row in set (0.02 sec)
mysql> SELECT instrument FROM instruments WHERE inst_id = 14;
+-----------+
| instrument|
+-----------+
| vocals |
+-----------+
1 row in set (0.00 sec)
Whew, that seems like a lot of work just to find the instruments that Lenny Kravitz plays, especially since this database was created to do just that kind of query. There must be a better way, right? Yes, there is another way.6 These four queries can be done in one query using a table join.
Table Joins
MySQL is a relational database, so we must be able to query information out of our database using information from more than one table. By joining the tables, we can use multiple tables in a single SELECT.
Again, the best way to talk about table joins is with an example. In the complex
SELECT
in the previous section, we discovered the instruments that Lenny Kravitz plays. We could have stated that request like this: “Give me all the
instrument
names in the
instruments
table that match the
inst_id
s in the
what_they_play
table for the
player_id
in the
musicians
table asso
ciated with the musician with the
name
Lenny Kravitz.”
The field
instrument
in the
instruments
table can be indicated in SQL with both the table name and the field name as
instruments.instrument
. Likewise,
inst_id
in
what_they_play
is
what_they_play.inst_id
. These fully qualified names allow us to use
player_id
in both the
musicians
table and the
what_they_play
table, and SQL can keep them separate because we will call them
musicians.player_id
and
what_they_play.player_id
.
Given that bit of good news, let’s translate the query we stated in English two paragraphs earlier into SQL:
mysql> SELECT instrument FROM instruments,what_they_play,musicians
-> WHERE instruments.inst_id = what_they_play.inst_id AND
-> what_they_play.player_id = musicians.player_id AND
-> musicians.name = "Lenny Kravitz";
+------------+
| instrument |
+------------+
| guitar |
| vocals |
+------------+
2 rows in set (0.00 sec)
The big difference in this query is that we have listed more than one table in the
FROM
part:
instruments
,
what_they_play
, and
musicians
. Then, our
WHERE
clause has several conditions all
AND
ed together. Since we are using more than one table and are comparing values in one table with the values in another, we are joining the data together. Hence the term table join.
Let’s do one more table join. Here is a query that will show all the musicians that play
percussion
instruments:
mysql> SELECT what_they_play.player_id FROM what_they_play,instruments
-> WHERE what_they_play.inst_id = instruments.inst_id AND
-> instruments.type = "percussion";
+-----------+
| player_id |
+-----------+
| 6 |
+-----------+
2 rows in set (0.00 sec)
As you can see, the
SELECT
combinations are endless! SQL is quite flexible—we can pull out exactly the information we need in exactly the order we want from the particular tables in which we are interested. We have only explored a few SQL commands—there are so many more to learn. Check out the documentation and Kofler’s book for more information.
Using the MySQL command line interface is enjoyable, but it is much more fun to query our database using Perl and
DBI
.