MySQL
  Home arrow MySQL arrow Page 6 - Using Subqueries In MySQL (part 1)
Dev Shed Forums 
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) - Having Your Code, And Eating It Too


    (Page 6 of 7 )

    You can use a subquery in a HAVING clause as well, and thereby use it as a filter for the groups created in the parent query. I'll demonstrate by finding out which sites are using more than 50% of all available services.




    mysql> SELECT bid FROM branches_services GROUP BY bid HAVING COUNT(sid)
    mysql> >
    (SELECT COUNT(*) FROM services)/2;
    +------+
    | bid |
    +------+
    | 1011 |
    +------+
    1 row in set (0.22 sec)

    You can add a fast inner join to get the branch name and customer name as well if you like:


    mysql> SELECT c.cid, c.cname, b.bid, b.bdesc FROM clients AS c, branches
    mysql> AS
    b, branches_services AS bs WHERE c.cid = b.cid AND b.bid = bs.bid GROUP BY bs.bid HAVING COUNT(bs.sid) > (SELECT COUNT(*) FROM services)/2;
    +-----+----------------+------+--------------+
    | cid | cname | bid | bdesc |
    +-----+----------------+------+--------------+
    | 101 | JV Real Estate | 1011 | Corporate HQ |
    +-----+----------------+------+--------------+
    1 row in set (0.28 sec)

    Is it possible to get a list of branches using all available services? Sure!


    mysql> SELECT branches.bid, COUNT(sid) FROM branches, branches_services
    WHERE branches.bid = branches_services.bid GROUP BY branches.bid HAVING
    COUNT(sid) = (SELECT COUNT(*) FROM services);
    Empty set (0.04 sec)

    And that's quite right - if you look at the raw data, you'll see that there are no individual branch offices using all available services. Maybe if I went up the totem pole a little and checked to see if there were any clients using all available services across their branch offices...


    mysql> SELECT clients.cname FROM clients, branches, branches_services
    mysql> WHERE
    branches.bid = branches_services.bid AND branches.cid = clients.cid GROUP BY clients.cid HAVING COUNT(sid) = (SELECT COUNT(*) FROM services);
    +----------------+
    | cname |
    +----------------+
    | JV Real Estate |
    +----------------+
    1 row in set (0.01 sec)

    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 6 hosted by Hostway
    Stay green...Green IT