MySQL
  Home arrow MySQL arrow MySQL Table Joins
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

MySQL Table Joins
By: W.J. Gilmore
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 109
    1999-07-06


    Table of Contents:
  • MySQL Table Joins
  • The Cross Join
  • The Equi-join
  • The Left Join
  • Self-joins

  • 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


    MySQL Table Joins
    ( Page 1 of 5 )

    One of the great characteristics of SQL (Structured Query Language) is the fact that one can develop, access and modify data across various tables. There are several benefits to this, including greater ease of manipulation, increased speed of access, and reduced data redundancy. In MySQL (as well as many other SQL languages), this is accomplished via the join command. One of the great characteristics of SQL (Structured Query Language) is the fact that one can develop access and modify data across various tables. There are several benefits to this, including greater ease of manipulation, increased speed of access, and reduced data redundancy. In MySQL (as well as many other SQL languages), this is accomplished via the join command.

    So what does a join do, anyway? In short, a join command unites some or all of the data from two or more tables into one comprehensive structure. If this capability was not available, then the administrator would be forced to create ever-expanding, all-encompassing tables that would result in the reduction of overall efficiency and speed of data access and manipulation, even eventually possibly causing the database structure to fail entirely. The joining capability allows the administrator to create small, compact relational tables that can greatly influence database performance. This is especially useful among larger organizations, given the fact that they have the opportunity to divide their database into several departmental sections, allowing each department to effectively manage their relevant data. The alternative to this would be to force one administrator to manage all departmental sections, something that probably does not bode well both in terms of overall organization and efficiency.

    For sake of illustration, let's assume a computer manufacturer is in need of a database in which it can efficiently mine data. This data will be divided into various compact tables, each specifying data relative to a certain concept, such as orders, clients and products. After the tables are created, various examples with be illustrated, showing how this manufacturer can use joins to make the most of the database.

    (Note to reader: While the concepts to follow could be applied to practically all SQL-based databases, it should be noted that the examples are illustrated using MySQL syntax. Therefore, there could be minor differences between this syntax and other sql-based languages.)

    The first table will hold the various types of PCs that the manufacturer distributes:


    mysql> create table pcs (     -> pid INT, // product id     -> spec char(3),     -> os char(10),     -> ram INT,     -> hd char(4)     -> );
    The second table will contain data regarding the various clients of the manufacturer:


    mysql> create table clients (     -> name char(25),     -> cid char(8), // client id     -> email char(25),     -> tel char(10)     -> );

    The third table will contain data regarding order information:


    mysql> create table orders (     -> order_date date,     -> pid INT,     -> cid char(8)     -> );

    Data Insertion

    We next insert data into each table:

    pcs
     
    pid spec os ram hd
    1 386 linux 64 3.1
    2 386 linux 128 4.2
    3 486 WinNT 64 3.1
    4 586 Linux 128 4.2
    5 586 Win98 128 6.4

    clients
     
    name cid email tel
    acme, inc. acm-042 acme@acme.com 123-456-7890
    widgets-r-us, inc. wig-043 widgets@rus.com 421-555-3434
    italimp, inc. imp-042 italian@imports.com 459-555-3212
    fedey, inc. fed-043 fed@ey.com 439-555-8899

    orders
     
    date pid cid
    1999-12-05 2 acm-042
    1999-12-04 3 wig-043
    1999-12-04 1 acm-042
    1999-12-05 2 acm-042
    1999-12-12 5 fed-043
    1999-12-05 5 imp-042


    So, how does one go about pulling useful data from these tables? What if the boss wanted a report listing the email addresses of all clients who ordered a particular pc on a particular date? Or perhaps the amount of RAM used in filling orders placed by a particular client? This is easily accomplished via the various joins available to us. Let's use the tables we have created and construct our first join statement.



     
     
    >>> More MySQL Articles          >>> More By W.J. Gilmore
     

       

    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