MySQL
  Home arrow MySQL arrow Page 5 - Using Subqueries In MySQL (part 1)
Administration  
AJAX  
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 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
MYSQL

Using Subqueries In MySQL (part 1)
By: RK Harigopal, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 11
    2003-07-24

    Table of Contents:
  • Using Subqueries In MySQL (part 1)
  • Sub-Zero Code
  • Turning The Tables
  • Back To Basics
  • Branching Out
  • Having Your Code, And Eating It Too
  • Apples And Oranges

  • 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


    Using Subqueries In MySQL (part 1) - Branching Out


    (Page 5 of 7 )

    Subqueries are usually preceded by a conditional WHERE clause, which can contain any of the following comparison and logical operators:


    Operator What It Means
    ---------------------------------------------
    = values are equal

    <> values are unequal

    <= value on left is less than or equal to value on right

    >= value on left is greater than or equal to value on right

    < value on left is less than value on right

    > value on left is greater than value on right

    BETWEEN value on left lies between values on right

    NOT logical NOT

    AND logical AND

    OR logical OR

    In order to demonstrate, let's say I wanted a list of all those customers with exactly two branch offices. First, I need to figure out a way to obtain the number of branch offices per customer,


    mysql> SELECT cid, COUNT(bid) FROM branches GROUP BY cid;
    +-----+------------+
    | cid | count(bid) |
    +-----+------------+
    | 101 | 3 |
    | 103 | 3 |
    | 104 | 2 |
    | 110 | 1 |
    +-----+------------+
    4 rows in set (0.27 sec)

    and then filter out those with just two offices with a HAVING clause,


    mysql> SELECT cid, COUNT(bid) FROM branches GROUP BY cid HAVING
    mysql> COUNT(bid)
    = 2;
    +-----+------------+
    | cid | count(bid) |
    +-----+------------+
    | 104 | 2 |
    +-----+------------+
    1 row in set (0.16 sec)

    and then hand the client ID over to the "clients" table in order to get the client name.


    mysql> SELECT cname FROM clients WHERE cid = 104;
    +------------------+
    | cname |
    +------------------+
    | Rabbit Foods Inc |
    +------------------+
    1 row in set (0.22 sec)

    The following subquery will take care of the three steps above for me:


    mysql> SELECT cname FROM clients WHERE cid = (SELECT cid FROM branches
    GROUP BY cid HAVING COUNT(bid) = 2);
    +------------------+
    | cname |
    +------------------+
    | Rabbit Foods Inc |
    +------------------+
    1 row in set (0.28 sec)

    In this case, the inner query is executed first - this query takes care of grouping the branches by customer ID and counting the number of records (branch offices) in each group. Those customers which have exactly two branch offices can easily filtered out with a HAVING clause, and the corresponding customer IDs returned to the main query, which then maps the IDs into the customers table and returns the corresponding customer name.

    How about selecting all those customers using the service with the maximum service fee?


    mysql> SELECT cname, bdesc FROM clients, branches, branches_services,
    services WHERE services.sid = branches_services.sid AND clients.cid = branches.cid AND branches.bid = branches_services.bid AND sfee = (SELECT
    MAX(sfee) FROM services);
    +----------------+--------------------------------+
    | cname | bdesc |
    +----------------+--------------------------------+
    | JV Real Estate | Customer Grievances Department |
    +----------------+--------------------------------+
    1 row in set (0.01 sec)

    Next up, HAVING clauses.

    More MySQL Articles
    More By RK Harigopal, (c) Melonfire


     

       

    MYSQL ARTICLES

    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL





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