MySQL
  Home arrow MySQL arrow Page 7 - Speaking SQL (part 2)
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

Speaking SQL (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2001-01-18

    Table of Contents:
  • Speaking SQL (part 2)
  • Christmas Presents
  • Teacher's Pet
  • Reading Backwards
  • Count() Me In
  • Like, You Know, Man...
  • Joining Them Together
  • Nest Egg

  • 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


    Speaking SQL (part 2) - Joining Them Together


    (Page 7 of 8 )

    So far, all the queries you've seen have been concentrated on a single table. But SQL also allows you to query two or more tables at a time, and display a combined result set. This is technically referred to as a "join", since it involves "joining" different tables at specific points to create new views of the data.

    When using a join, it's recommended that you prefix each column name with the name of the table it belongs to (I haven't done this in any of the examples you've seen so far because all the columns have been localized to a single table.). For example, you would use "members.fname" to refer to the column named "fname" in the table "members", and "status.video_id" to refer to the "video_id" column in the "status" table.

    Here's an example of a simple join:

    mysql> SELECT * FROM status, members WHERE status.member_id = members.member_id; +-----------+----------+-----------+-------+-------+---------+-------------- --------------+ | member_id | video_id | member_id | fname | lname | tel | email | +-----------+----------+-----------+-------+-------+---------+-------------- --------------+ | 1 | 1 | 1 | John | Doe | 1234567 | jdoe@somewhere.com | | 1 | 2 | 1 | John | Doe | 1234567 | jdoe@somewhere.com | | 1 | 3 | 1 | John | Doe | 1234567 | jdoe@somewhere.com | | 2 | 6 | 2 | Jane | Doe | 8373728 | jane@site.com | | 4 | 2 | 4 | Santa | Claus | 9999999 | santa@the-north-pole.com | | +-----------+----------+-----------+-------+-------+---------+-------------- --------------+ 5 rows in set (0.00 sec)
    In this case, the "status" and "members" tables have been joined together through the common column "member_id".

    You can specify the columns you'd like to see from the joined tables, as with any SELECT statement:

    mysql> SELECT fname, lname, video_id FROM members, status WHERE members.member_id = status.member_id; +-------+-------+----------+ | fname | lname | video_id | +-------+-------+----------+ | Jane | Doe | 6 | | Santa | Claus | 2 | | John | Doe | 1 | | John | Doe | 2 | | John | Doe | 3 | +-------+-------+----------+ 5 rows in set (0.16 sec)
    You can also join three tables together - the following example uses the "status" table, combined with member information and video details, to create a composite table which displays which members have which videos.

    mysql> SELECT fname, lname, title FROM members, videos, status WHERE status.member_id = members.member_id AND status.video_id = videos.video_id; +-------+-------+-------------------------------+ | fname | lname | title | +-------+-------+-------------------------------+ | Jane | Doe | Woman On Top | | Santa | Claus | ET | | John | Doe | Star Wars: The Phantom Menace | | John | Doe | ET | | John | Doe | Charlie's Angels | +-------+-------+-------------------------------+ 5 rows in set (0.17 sec)
    Incidentally, if the thought of writing long table names over and over again doesn't appeal to you, you can assign simple aliases to each table and use these instead. The following example assigns the aliases "m", "s" and "v" to the "members", "status" and "videos" tables respectively.

    mysql> SELECT m.fname, m.lname, v.title FROM members m, status s, videos v WHERE s.member_id = m.member_id AND s.video_id = v.video_id; +-------+-------+-------------------------------+ | fname | lname | title | +-------+-------+-------------------------------+ | Jane | Doe | Woman On Top | | Santa | Claus | ET | | John | Doe | Star Wars: The Phantom Menace | | John | Doe | ET | | John | Doe | Charlie's Angels | +-------+-------+-------------------------------+ 5 rows in set (0.00 sec)


    This article copyright Melonfire 2001. All rights reserved.

    More MySQL Articles
    More By icarus, (c) Melonfire


     

       

    MYSQL ARTICLES

    - 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
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway