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

 
Developer Updates  
Free Website Content 
 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? 
Google.com  
PERL

SELECT Queries and Perl
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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 .



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

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek