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

Understanding SQL Joins
By: The Disenchanted Developer, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 229
    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:
      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


    Understanding SQL Joins - The Bookworm Turns
    ( Page 8 of 10 )

    So that's the theory - now let's see how it plays out in real life. Consider the following tables, which contain data for a small lending library:

    +----+-------------------------------------+ | id | title | +----+-------------------------------------+ | 1 | Mystic River | | 2 | Catch-22 | | 3 | Harry Potter and the Goblet of Fire | | 4 | The Last Detective | | 5 | Murder on the Orient Express | +----+-------------------------------------+
    This table, named "books", contains a complete list of books available for lending. Each book has a unique ID.

    +-----+-------+ | id | name | +-----+-------+ | 100 | Joe | | 101 | John | | 103 | Dave | | 109 | Harry | | 110 | Mark | | 113 | Jack | +-----+-------+
    This table, named "members", contains a list of all the members of the library. Like the books, each member also has a unique ID.

    +-----------+---------+ | member_id | book_id | +-----------+---------+ | 101 | 1 | | 103 | 3 | | 109 | 4 | +-----------+---------+
    As members borrow books from the library, the "data" table, above, is updated with information on which book has been lent to which member.

    Using these tables and whatever you've just learned about joins, it's possible to write queries that answer the most common questions asked of the system by its operators. For example, let's suppose I wanted a list of all the current members:

    SELECT * FROM members; +-----+-------+ | id | name | +-----+-------+ | 100 | Joe | | 101 | John | | 103 | Dave | | 109 | Harry | | 110 | Mark | | 113 | Jack | +-----+-------+
    How about a list of all the books currently in the library catalog?

    SELECT * FROM books; +----+-------------------------------------+ | id | title | +----+-------------------------------------+ | 1 | Mystic River | | 2 | Catch-22 | | 3 | Harry Potter and the Goblet of Fire | | 4 | The Last Detective | | 5 | Murder on the Orient Express | +----+-------------------------------------+
    How about an enquiry on Dave's account, to see which books he's currently checked out?

    SELECT books.title FROM data,books WHERE data.book_id = books.id AND data.member_id = 103; +-------------------------------------+ | title | +-------------------------------------+ | Harry Potter and the Goblet of Fire | +-------------------------------------+
    Can we find out which members *don't* have any books checked out to them (maybe these are inactive accounts)?

    SELECT members.id, members.name FROM members LEFT JOIN data ON data.member_id = members.id WHERE data.book_id IS NULL; +-----+------+ | id | name | +-----+------+ | 100 | Joe | | 110 | Mark | | 113 | Jack | +-----+------+
    How about a list of all the books that are currently in stock (not checked out to anyone)?

    SELECT books.title FROM books LEFT JOIN data ON data.book_id = books.id WHERE data.book_id IS NULL +------------------------------+ | title | +------------------------------+ | Catch-22 | | Murder on the Orient Express | +------------------------------+
    Now, how about a list of all the books currently checked out, together with the name of the member they've been checked out to?

    SELECT members.name, books.title FROM data, members, books WHERE data.member_id = members.id AND data.book_id = books.id; +-------+-------------------------------------+ | name | title | +-------+-------------------------------------+ | John | Mystic River | | Dave | Harry Potter and the Goblet of Fire | | Harry | The Last Detective | +-------+-------------------------------------+
    You could also accomplish the above with the following query (an outer join instead of an inner join):

    SELECT members.name, books.title FROM books,members LEFT JOIN data ON data.book_id = books.id WHERE data.member_id = members.id AND data.book_id IS NOT NULL;
    As you can see, joins allow you to extract all kinds of different information from a set of tables. Go ahead and try it for yourself - think of a question you would like to ask this system, and then write a query to answer it.

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

       

    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
    For more Enterprise Application Development news, visit eWeek