MySQL
  Home arrow MySQL arrow Page 6 - Using Subqueries In MySQL (part 2)
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  
MYSQL

Using Subqueries In MySQL (part 2)
By: RK Harigopal, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 16
    2003-07-31


    Table of Contents:
  • Using Subqueries In MySQL (part 2)
  • Total Recall
  • In And Out
  • A Solitary Existence
  • Turning The Tables
  • Show Me The Money
  • Adjusting For Inflation
  • A New Broom
  • Cleaning Up

  • 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


    Using Subqueries In MySQL (part 2) - Show Me The Money
    ( Page 6 of 9 )

    Thus far, every single example you've seen in this tutorial has involved a subquery within an outer SELECT statement. However, subqueries can be used in other places too - most notably in UPDATE and DELETE statements, which can use the results of a subquery to constrain the records to which the UPDATE or DELETE is applied.

    I'll explain this with a simple example. Let's suppose you want to delete all branches using the "Recruitment" service (service ID 2). Normally, you'd first look up the branches using that service in the "branches_services" table,




    mysql> SELECT * FROM branches_services WHERE sid = 2;
    +------+-----+
    | bid | sid |
    +------+-----+
    | 1011 | 2 |
    | 1031 | 2 |
    +------+-----+
    2 rows in set (0.16 sec)

    and then delete the corresponding branch records from the "branches" table.


    mysql> DELETE FROM branches WHERE bid = 1011;
    Query OK, 1 row affected (0.00 sec)

    mysql> DELETE FROM branches WHERE bid = 1031;
    Query OK, 1 row affected (0.00 sec)

    You can combine the two operations above into a single one with a subquery, as below:


    mysql> DELETE FROM branches WHERE bid IN (SELECT bid FROM
    mysql> branches_services
    WHERE sid = 2);
    Query OK, 2 rows affected (0.00 sec)

    A check of the "branches" table confirms that the operation was successful.


    mysql> SELECT * FROM branches;
    +------+-----+--------------------------------+------+
    | bid | cid | bdesc | bloc |
    +------+-----+--------------------------------+------+
    | 1012 | 101 | Accounting Department | NY |
    | 1013 | 101 | Customer Grievances Department | KA |
    | 1041 | 104 | Branch Office (East) | MA |
    | 1042 | 104 | Branch Office (West) | CA |
    | 1101 | 110 | Head Office | CA |
    | 1032 | 103 | NE Region HO | CT |
    | 1033 | 103 | NW Region HO | NY |
    +------+-----+--------------------------------+------+
    7 rows in set (0.00 sec)

    How about deleting all those customers, any of whose branch offices generate service fee revenues of $500 or less?


    mysql> DELETE FROM clients WHERE cid IN (SELECT DISTINCT b.cid FROM
    branches AS b, branches_services AS bs, services AS s WHERE b.bid = bs.bid AND bs.sid = s.sid GROUP BY bs.bid HAVING SUM(sfee) <= 500); Query OK, 1 row affected (0.28 sec)

    In this case, the inner query groups the various branches by branch ID, calculates the total service generated by each branch for all the services its using, and lists those records where the total is less than or equal to $500. The corresponding customer IDs are then used by the outer query to perform a DELETE operation on the "clients" table.



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

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - 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...





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