MySQL
  Home arrow MySQL arrow Page 7 - 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 
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 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) - Adjusting For Inflation


    (Page 7 of 9 )

    You can use subqueries in an UPDATE statement in much the same manner. Let's suppose I wanted to find out which services are in use in 3 or more branch offices,




    mysql> SELECT sid FROM branches_services GROUP BY sid HAVING COUNT(bid)
    mysql> >= 3;
    +-----+
    | sid |
    +-----+
    | 1 |
    | 3 |
    | 4 |
    +-----+
    3 rows in set (0.11 sec)

    and then increase the fee for those services by 25% (hey, those weekly yacht parties don't come cheap!).


    mysql> UPDATE services SET sfee = sfee + (sfee * 0.25) WHERE sid = 1;
    Query OK, 1 row affected (0.05 sec)
    Rows matched: 1 Changed: 1 Warnings: 0

    mysql> UPDATE services SET sfee = sfee + (sfee * 0.25) WHERE sid = 3;
    Query OK, 1 row affected (0.05 sec)
    Rows matched: 1 Changed: 1 Warnings: 0

    mysql> UPDATE services SET sfee = sfee + (sfee * 0.25) WHERE sid = 4;
    Query OK, 1 row affected (0.05 sec)
    Rows matched: 1 Changed: 1 Warnings: 0

    I could combine the operations above into the following subquery statement:


    mysql> UPDATE services SET sfee = sfee + (sfee * 0.25) WHERE sid IN
    mysql> (SELECT
    sid FROM branches_services GROUP BY sid HAVING COUNT(bid) >= 3); Query OK, 3 rows affected (0.22 sec) Rows matched: 3 Changed: 3 Warnings: 0

    Let's take another example. Let's suppose I wanted to have all branches located in California use the "Security" service instead of the "Administration" service. With a subquery, it's a piece of cake:


    mysql> UPDATE branches_services SET sid = 6 WHERE sid = 4 AND bid IN
    (SELECT bid FROM branches WHERE bloc = 'CA');
    Query OK, 2 rows affected (0.00 sec)
    Rows matched: 2 Changed: 2 Warnings: 0

    In this case, the inner query takes care of isolating only those branch IDs in California, and provides this list to the outer query, which updates the corresponding records in the "branches_services" table. Notice how I've split the selection criteria for the rows to be UPDATEd: the inner query lists the records for California, the outer one further winnows it down to those using just the "Administration" service.

    Wanna make it even more complicated? Add subqueries to the various SET clauses as well.


    mysql> UPDATE branches_services SET sid = (SELECT sid FROM services
    mysql> WHERE
    sname = 'Security') WHERE sid = (SELECT sid FROM services WHERE sname =
    'Administration') AND bid IN (SELECT bid FROM branches WHERE bloc = 'CA'); Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0

    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