5.4. Table Joins

In the world of relational databases, data often has complex relationships and is spread across multiple tables. Sometimes it is necessary to grab information from one table based on information in another. This requires that the two tables be JOINed.

For an example, we create a new table in the people database called addresses that contains information about people’s addresses (surprise!). First, it must be created as follows:

mysql> CREATE TABLE addresses ( 
    ->     lastname CHAR(20), 
    ->     firstname CHAR(20), 
    ->     address CHAR(40), 
    ->     city CHAR(20), 
    ->     state CHAR(2), 
    ->     zip CHAR(10) 
    -> ); 

The table needs some data:

mysql> INSERT INTO addresses 
    ->    (lastname, firstname, address, city, state, zip) 
    ->    VALUES ("Wall", "Larry", "Number ...

Get Open Source Web Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.