MySQL
  Home arrow MySQL arrow Page 5 - Understanding SQL 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

Understanding SQL Joins
By: The Disenchanted Developer, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 216
    2002-08-20

    Table of Contents:
  • Understanding SQL Joins
  • Meeting The Family
  • Keeping It Simple
  • Crossed Wires
  • Finding Common Ground
  • One Step Left...
  • ...Two Steps Right
  • The Bookworm Turns
  • Up A Tree
  • A Long Goodbye

  • 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


    Understanding SQL Joins - Finding Common Ground


    (Page 5 of 10 )

    Since a cross join produces a huge resultset, it is considered a good idea to attach a WHERE clause to the join to filter out some of the records. Here's an example:

    SELECT * FROM a,b WHERE a1 > 30;
    This returns

    +----+------+----+------+ | a1 | a2 | b1 | b2 | +----+------+----+------+ | 40 | x | 10 | p | | 50 | y | 10 | p | | 60 | z | 10 | p | | 40 | x | 20 | q | | 50 | y | 20 | q | | 60 | z | 20 | q | +----+------+----+------+ 6 rows in set (0.06 sec)
    A variant of the cross join is the "equi-join", where certain fields in the joined tables are equated to each other. In this case, the final resultset will only include those rows from the joined tables which have matches in the specified fields.

    SELECT * FROM a,b WHERE a1 = b1;
    This returns

    +----+------+----+------+ | a1 | a2 | b1 | b2 | +----+------+----+------+ | 10 | u | 10 | p | | 20 | v | 20 | q | +----+------+----+------+ 2 rows in set (0.00 sec)
    Here, too, you can use a WHERE clause to further narrow the resultset:

    SELECT * FROM a,b WHERE a1 = b1 AND a1 = 20;
    This returns

    +----+------+----+------+ | a1 | a2 | b1 | b2 | +----+------+----+------+ | 20 | v | 20 | q | +----+------+----+------+ 1 row in set (0.00 sec)

    More MySQL Articles
    More By The Disenchanted Developer, (c) Melonfire


     

       

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