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

MySQL Table Joins
By: W.J. Gilmore
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 90
    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:
      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


    MySQL Table Joins - The Left Join


    (Page 4 of 5 )

    The left join allows the user to pull out all sorts of interesting data based upon certain restrictions. This is an extremely powerful option of table joins, and greatly facilitates table manipulation.

    Now, the boss is screaming for some details. Details, details, details! It's no problem however, as the left join will solve our problem.

    Assume that


    mysql> select * from orders left join pcs on orders.pid = pcs.pid";

    order_datepidcidpidspecosramhd
    1999-12-052acm-0422386linux1284.2
    1999-12-043wig-0433486WinNT643.1
    1999-12-041acm-0421386linux643.1
    1999-12-052acm-0422386linux1284.2
    1999-12-125fed-0435586Win981286.4
    1999-12-055imp-0425586Win981286.4

    We now have a informative listing of all pcs ordered by our clients! Using a PHP3 or Perl script, one could see how this could be used to print out receipts, for example. We could combine this with the client table for reason of emailing the client an occasional email with a list of all products he has purchased from our company.

    Perhaps another useful report we could generate would involve learning of the number of pcs ordered that had product id (pid) number 3.


    mysql> select * from orders left join pcs on pcs.pid = 3 and orders.pid = pcs.pid;

    order_datepidcidpidspecosramhd
    1999-12-052acm-0422nullnullnullnull
    1999-12-043wig-0433486WinNT643.1
    1999-12-041acm-0421nullnullnullnull
    1999-12-052acm-0422nullnullnullnull
    1999-12-125fed-0435nullnullnullnull
    1999-12-055imp-0425nullnullnullnull

    The Using Clause

    A variation to the left join allows us to further correlate identical columns residing in multiple tables. This is the using option. Instead of the following:


    mysql> SELECT * from clients join on orders where clients.cid = orders.cid;

    We could state:


    mysql> SELECT * from clients join on orders using (cid);

    However, both would produce the same result:

    namecidemailtelorder_datepidcid
    acme, inc.acm-042acme@acme.com123-465-7891999-12-052acm-042
    acme, inc.acm-042acme@acme.com123-465-7891999-12-041acm-042
    acme, inc.acm-042acme@acme.com123-465-7891999-12-052acm-042
    widgets-r-us, inc.wig-043widgets@rus.com421-555-34341999-12-043wig-043
    italimp, inc.imp-042widgets@rus.com459-555-34341999-12-055imp-042
    fedey, inc.fed-043fed@ey.com439-555-88991999-12-125fed-043

    There you have it. Table joins made easy. Try playing around with variations of the commands highlighted within this article to gain a clear understanding of the syntax. Once this is understood, you will find that table joins will play an integral part in your development activities. Be sure to check out MySQL's various discussion groups (http://www.mysql.com), as there is usually quite a bit of information exchanged regarding table joins.

    More MySQL Articles
    More By W.J. Gilmore


     

       

    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 5 hosted by Hostway
    Stay green...Green IT