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 hisplayer_idis by querying themusicianstable; then using thatplayer_id, select theinst_ids out ofwhat_they_play; then for each of thoseinst_ids, we can then get theinstrumentname out of theinstrumentstable.
First, get Lenny Kravitz’splayer_id:
mysql> SELECT player_id FROM musicians WHERE name = "Lenny Kravitz";
Now, using hisplayer_idof 5, grab theinst_ids out ofwhat_they_play:
mysql> SELECT inst_id FROM what_they_play WHERE player_id = 5;
And the last step is, for each of theinst_ids, 11 and 14, query theinstrumentstable for theinstrument:
mysql> SELECT instrument FROM instruments WHERE inst_id = 11;
mysql> SELECT instrument FROM instruments WHERE inst_id = 14;
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.
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 complexSELECTin the previous section, we discovered the instruments that Lenny Kravitz plays. We could have stated that request like this: “Give me all theinstrumentnames in theinstrumentstable that match theinst_ids in thewhat_they_playtable for theplayer_idin themusicianstable associated with the musician with thenameLenny Kravitz.”
The fieldinstrumentin theinstrumentstable can be indicated in SQL with both the table name and the field name asinstruments.instrument. Likewise,inst_idinwhat_they_playiswhat_they_play.inst_id. These fully qualified names allow us to useplayer_idin both themusicianstable and thewhat_they_playtable, and SQL can keep them separate because we will call themmusicians.player_idandwhat_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
The big difference in this query is that we have listed more than one table in theFROMpart:instruments,what_they_play, andmusicians. Then, ourWHERE clause has several conditions allANDed 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 playpercussioninstruments:
mysql> SELECT what_they_play.player_id FROM what_they_play,instruments
As you can see, theSELECTcombinations 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 andDBI.
blog comments powered by Disqus