Umm... I'm not sure I can explain it any clearer than my article does... but I'll take a shot. Say you have two tables:
Code:
Articles
+----+----------+----------+
| ID | Title | AuthorID |
+----+----------+----------+
| 1 | Article1 | 1 |
| 2 | Article2 | 1 |
| 3 | Article3 | 2 |
+----+----------+----------+
Authors
+----+-------+
| ID | Name |
+----+-------+
| 1 | Kevin |
| 2 | Matt |
+----+-------+
So our database contains three articles and two authors. Articles 1 and 2 are written by author 1 (Kevin), and Article 3 is written by author 2 (Matt).
Now, say we wanted to display a Web page listing the articles and their authors. We'd need elements of both tables. The best way to do that is to create a SELECT statement that joins rows from the two tables involved.
The most basic join would be the following:
SELECT * FROM Articles, Authors
This combines all of the rows from the Articles table with all the rows from the Authors table:
Code:
+----+----------+----------+----+-------+
| ID | Title | AuthorID | ID | Name |
+----+----------+----------+----+-------+
| 1 | Article1 | 1 | 1 | Kevin |
| 1 | Article1 | 1 | 2 | Matt |
| 2 | Article2 | 1 | 1 | Kevin |
| 2 | Article2 | 1 | 2 | Matt |
| 3 | Article3 | 2 | 1 | Kevin |
| 3 | Article3 | 2 | 2 | Matt |
+----+----------+----------+----+-------+
Now, this table doesn't make a lot of sense... What we want is to link the articles with their authors. To do this, we limit the rows displayed to those where the AuthorID from the Articles table matches the ID from the Authors table:
SELECT * FROM Articles, Authors WHERE Articles.AuthorID=Authors.ID
Code:
+----+----------+----------+----+-------+
| ID | Title | AuthorID | ID | Name |
+----+----------+----------+----+-------+
| 1 | Article1 | 1 | 1 | Kevin |
| 2 | Article2 | 1 | 1 | Kevin |
| 3 | Article3 | 2 | 2 | Matt |
+----+----------+----------+----+-------+
There we go! Each article has been listed alongside its author. Now, all we have to do is strip out the information we don't need by selecting only those columns we're interested in:
SELECT Title, Name FROM Articles, Authors WHERE Articles.AuthorID=Authors.ID
Code:
+----------+-------+
| Title | Name |
+----------+-------+
| Article1 | Kevin |
| Article2 | Kevin |
| Article3 | Matt |
+----------+-------+
Ta da! This result set is a join of our two tables.
Bookmarks