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