MySQL
  Home arrow MySQL arrow Page 2 - 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) - Total Recall
    ( Page 2 of 9 )

    Before we begin, a quick recap and re-introduction to the tables I'll be using throughout this tutorial seems to be in order. In order to explain subqueries, I'll be using a fictitious company's accounting database, which consists of the following four tables:

    1. The "services" table: The fictitious system under discussion consists of a service company's accounting database. This company offers customers a number of outsourced services, each of which is associated with a fee and has a unique service ID. This information is stored in a "services" table, which looks like this:


    mysql> SELECT * FROM services;
    +-----+------------------+---------+
    | sid | sname | sfee |
    +-----+------------------+---------+
    | 1 | Accounting | 1500.00 |
    | 2 | Recruitment | 500.00 |
    | 3 | Data Management | 300.00 |
    | 4 | Administration | 500.00 |
    | 5 | Customer Support | 2500.00 |
    | 6 | Security | 600.00 |
    +-----+------------------+---------+
    6 rows in set (0.16 sec)

    2. The "clients" table: The company also has a list of its current clients stored in a separate "clients" table. Each client is identified with a unique customer ID.


    mysql> SELECT * FROM clients;
    +-----+-----------------------------+
    | cid | cname |
    +-----+-----------------------------+
    | 101 | JV Real Estate |
    | 102 | ABC Talent Agency |
    | 103 | DMW Trading |
    | 104 | Rabbit Foods Inc |
    | 110 | Sharp Eyes Detective Agency |
    +-----+-----------------------------+
    5 rows in set (0.00 sec)

    3. The "branches" table: Each customer may have one or more branch offices. The "branches" table lists the branch offices per customer, together with each branch's location. Each branch has a description, a unique branch ID, and a foreign key reference to the customer ID.


    mysql> SELECT * FROM branches;
    +------+-----+--------------------------------+------+
    | bid | cid | bdesc | bloc |
    +------+-----+--------------------------------+------+
    | 1011 | 101 | Corporate HQ | CA |
    | 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 |
    | 1031 | 103 | N Region HO | ME |
    | 1032 | 103 | NE Region HO | CT |
    | 1033 | 103 | NW Region HO | NY |
    +------+-----+--------------------------------+------+
    9 rows in set (0.01 sec)

    4. The "branches_services" table: Services supplied to each branch office are listed in this table, which contains pairs of branch IDs and service IDs (foreign keys into the "branches" and "services" table respectively).


    mysql> SELECT * FROM branches_services;
    +------+-----+
    | bid | sid |
    +------+-----+
    | 1011 | 1 |
    | 1011 | 2 |
    | 1011 | 3 |
    | 1011 | 4 |
    | 1012 | 1 |
    | 1013 | 5 |
    | 1041 | 1 |
    | 1041 | 4 |
    | 1042 | 1 |
    | 1042 | 4 |
    | 1101 | 1 |
    | 1031 | 2 |
    | 1031 | 3 |
    | 1031 | 4 |
    | 1032 | 3 |
    | 1033 | 4 |
    +------+-----+
    16 rows in set (0.11 sec)

    In order to use the examples in this tutorial, you'll need to recreate these tables in your MySQL 4.1 database server. Instructions and SQL code for doing so are available in the first part of this article. Once you're done reading that, come back and let's continue.



     
     
    >>> 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek