Perl
  Home arrow Perl arrow Page 3 - SELECT Queries and Perl
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

SELECT Queries and Perl
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 3
    2008-03-27

    Table of Contents:
  • SELECT Queries and Perl
  • The ORDER BY Clause
  • More Complicated SELECTs
  • Introduction to DBI

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Virtual Tradeshows by Ziff Davis Enterprise - A Unique Opportunity to Connect with IT Experts, Access Information, and Gain Insight on today's Technology

    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 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 Articles
    More By Apress Publishing


     

    Buy this book now. This article is excerpted from chapter 15 of the book Beginning Perl (Apress; ISBN: 159059391X). Check it out today at your favorite bookstore. Buy this book now.

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway