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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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 2)
By: RK Harigopal, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    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:
      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 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

    - 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
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...





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