Home arrow MySQL arrow Page 7 - Speaking SQL (part 2)

Joining Them Together - MySQL

After learning how to insert and edit data in a database, thesecond part of our SQL tutorial takes an in-depth look at the SELECTquery,and explains how to use joins, sub-queries and built-in functions tofocus in on the data you need.

TABLE OF CONTENTS:
  1. Speaking SQL (part 2)
  2. Christmas Presents
  3. Teacher's Pet
  4. Reading Backwards
  5. Count() Me In
  6. Like, You Know, Man...
  7. Joining Them Together
  8. Nest Egg
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 5
January 18, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MYSQL ARTICLES

- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: