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  
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 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";

    namecid
    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 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