Home arrow Perl Programming arrow Page 3 - SELECT Queries and Perl

More Complicated SELECTs - Perl

In this third part of a four-part series on Perl and DBI, you will learn about using SELECT queries to get information from databases. This article is excerpted from chapter 15 of the book Beginning Perl (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. SELECT Queries and Perl
  2. The ORDER BY Clause
  3. More Complicated SELECTs
  4. Introduction to DBI
By: Apress Publishing
Rating: starstarstarstarstar / 5
March 27, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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";
+-----------+
| player_id |
+-----------+
|         5 |
+-----------+
1 row in set (0.00 sec)

Now, using hisplayer_idof 5, grab theinst_ids out ofwhat_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 theinst_ids, 11 and 14, query theinstrumentstable for theinstrument:

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 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
   ->   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 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 
  
->     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, 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.



 
 
>>> More Perl Programming Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: