Perl
  Home arrow Perl arrow 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 Developerworks
 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

    Create a Highly Available Database Solution. Advantage Database Server can provide the availability to keep your data safe and ready for your application and users.

    SELECT Queries and Perl
    (Page 1 of 4 )

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

    The WHERE Clause

    So far, all of ourSELECT queries have shown every row in the table. Oftentimes we want only specific rows. TheWHEREclause tells theSELECTquery to show only the rows that match our criteria. For instance, we may want to see all the information in themusicianstable for the musician withplayer_idhaving the value 1.

    mysql> SELECT * FROM musicians WHERE player_id = 1;
    +-----------+--------------+----------+
    | player_id | name         | phone    |
    +-----------+--------------+----------+
    |         1 | Roger Waters | 555-1212 |
    +-----------+--------------+----------+
    1 row in set (0.00 sec)

    Or, how about selecting only the name of the musician withplayer_id1.

    mysql> SELECT name FROM musicians WHERE player_id = 1;
    +--------------+
    | name         |
    +--------------+
    | Roger Waters |
    +--------------+
    1 row in set (0.00 sec)

    Here’s how to grab Thom Yorke’s phone number:

    mysql> SELECT phone FROM musicians WHERE name = "Thom Yorke";
    +----------+
    | phone    |
    +----------+
    | 555-4545 |
    +----------+
    1 row in set (0.00 sec)

    Maybe we are interested in all instruments with difficulties of 8 or higher:

    mysql> SELECT instrument FROM instruments WHERE difficulty >= 8;
    +------------+
    | instrument |
    +------------+
    | bagpipes   |
    | oboe       |
    | harp       |
    +------------+
    3 rows in set (0.00 sec)

    Or the easiest instruments:

    mysql> SELECT instrument FROM instruments WHERE difficulty <= 2;
    +--------------+
    | instrument   |
    +--------------+
    | keyboards    |
    | drums        |
    | conductor    |
    +--------------+
    3 rows in set (0.00 sec)

    More than one condition can be combined. Here is aSELECTquery that returns all the percussion instruments with a difficulty less than or equal to 3:

    mysql> SELECT instrument FROM instruments

       ->   WHERE type = "percussion" AND difficulty <= 3;
    +------------+
    | instrument |
    +------------+
    | drums      |
    +------------+
    2 rows in set (0.00 sec)


    Note  There are many different ways to use theWHERE clause in theSELECT. See the docs for all the details.


    We could go on forever with describing all the different uses of theWHEREclause, but we should instead mention how to sort the output.

    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: 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
    - Perl: Releasing Your Inner Textuality




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