MySQL
  Home arrow MySQL arrow Page 2 - 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? 
MYSQL

MySQL Table Joins
By: W.J. Gilmore
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 108
    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 - The Cross Join
    ( Page 2 of 5 )

    The cross-join is a very basic type of join that simply matches each row from one table to every row from another table. While not very efficient, it does illustrate the common characteristic of all joins: table unification.

    An example of a cross-join would be as follows:


    mysql> SELECT * FROM pcs, clients;

    So why are there so many rows? Remember that a cross-join matches up each row of one table with every row of the second table. Therefore, a join involving two tables in which one table has 3 rows and the second table has 4 rows would result in 12 rows.

    Perhaps it is easy to remember the cross-join by putting the result in the perspective that each row in table1 crosses every row in table 2 once.

    Voilą! You have successfully performed your first join! Not so hard after all, is it?

    Now try the following and see what happens:


    mysql> select c.name, o.cid from orders o, clients c where o.cid = "acm-042";

    name cid
    acme, Inc. acm-042
    acme, Inc. acm-042
    acme, Inc. acm-042
    widgets-r-us, inc. acm-042
    widgets-r-us, inc. acm-042
    widgets-r-us, inc. acm-042
    italimp, inc. acm-042
    italimp, inc. acm-042
    italimp, inc. acm-042
    fedey, inc. acm-042
    fedey, inc. acm-042
    fedey, inc. acm-042

    So what did you think about the result? Was it what you expected it to be? This time, each name in table clients was matched to every row in orders containing "acm-042". It still fits our above explanation of a cross-join, yet it is more selective in which rows are "crossed". Try playing around with the order of the query, and see what happens. Notice that when the table order is switched, the output will change accordingly.

    Note: Why are aliases used when naming tables? Aliases are used as a method of saving repetitious keystrokes when entering queries. Thus, instead of repeatedly spelling out 'clients' when specifying columns, we could use 'c' and then state 'from clients c' within our query.

    Although the cross-join does in fact bring our tables together, it is not very efficient. So how can we sparingly choose the data we want from each table? Read on to find out more.



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