Selecting from Multiple Tables
You’re not limited to selecting only one table at a time. That would certainly make application programming a long and tedious task! When you select from more than one table in one SELECT statement, you are said to be joining the tables together.
Suppose you have two tables, fruit and color. You can select all rows from each of the two tables, using two separate SELECT statements:
mysql> select * from fruit;
+—-+———–+
| id | fruitname |
+—-+———–+
| 1 | apple |
| 2 | orange |
| 3 | grape |
| 4 | banana |
+—-+———–+
4 rows in set (0.00 sec)
mysql> select * from color;
+—-+———–+
| id | colorname |
+—-+———–+
| 1 | red |
| 2 | orange |
| 3 | purple |
| 4 | yellow |
+—-+———–+
4 rows in set (0.00 sec)
When you want to select from both tables at once, there are a few differences in the syntax of the SELECT statement. First, you must ensure that all the tables you’re using in your query appear in the FROM clause of the SELECT statement. Using the fruit and color example, if you simply want to select all columns and rows from both tables, you might think you would use the following SELECT statement:
mysql> select * from fruit, color;
+—-+———–+—-+———–+
| id | fruitname | id | colorname |
+—-+———–+—-+———–+
| 1 | apple | 1 | red |
| 2 | orange | 1 | red |
| 3 | grape | 1 | red |
| 4 | banana | 1 | red |
| 1 | apple | 2 | orange |
| 2 | orange | 2 | orange |
| 3 | grape | 2 | orange |
| 4 | banana | 2 | orange |
| 1 | apple | 3 | purple |
| 2 | orange | 3 | purple |
| 3 | grape | 3 | purple |
| 4 | banana | 3 | purple |
| 1 | apple | 4 | yellow |
| 2 | orange | 4 | yellow |
| 3 | grape | 4 | yellow |
| 4 | banana | 4 | yellow |
+—-+———–+—-+———–+
16 rows in set (0.00 sec)
Sixteen rows of repeated information is probably not what you were going for! What this query did is literally join a row in the color table to each row in the fruit table. Because there are four records in the fruit table and four entries in the color table, that’s 16 records returned to you.
When you select from multiple tables, you must build proper WHERE clauses to ensure you really get what you want. In the case of the fruit and color tables, what you really want is to see the fruitname and colorname records from these two tables where the IDs of each match up. This brings us to the next nuance of the query—how to indicate exactly which field you want when the fields are named the same in both tables!
Simply, you append the table name to the field name, like this:
tablename.fieldname
So, the query for selecting fruitname and colorname from both tables where the IDs match would be
mysql> select fruitname, colorname from fruit, color where fruit.id = color.id;
+———–+———–+
| fruitname | colorname |
+———–+———–+
| apple | red |
| orange | orange |
| grape | purple |
| banana | yellow |
+———–+———–+
4 rows in set (0.00 sec)
However, if you attempt to select a column that appears in both tables with the same name, you will get an ambiguity error:
mysql> select id, fruitname, colorname from fruit, color
-> where fruit.id = color.id;
ERROR 1052: Column: ‘id’ in field list is ambiguous
If you mean to select the id from the fruit table, you would use
mysql> select fruit.id, fruitname, colorname from fruit,
-> color where fruit.id = color.id;
+——+———–+———–+
| id | fruitname | colorname |
+——+———–+———–+
| 1 | apple | red |
| 2 | orange | orange |
| 3 | grape | purple |
| 4 | banana | yellow |
+——+———–+———–+
4 rows in set (0.00 sec)
Using JOIN
Several types of JOINs can be used in MySQL, all of which
refer to the order in which the tables are put together and the
results are displayed.
The type of JOIN used with the fruit and color tables is called
an INNER JOIN, although it wasn’t written explicitly as such.
To rewrite the SQL statement using the proper INNER JOIN syntax,
you would use
mysql> select fruitname, colorname from fruit inner join color
-> on fruit.id = color.id;
+———–+———–+
| fruitname | colorname |
+———–+———–+
| apple | red |
| orange | orange |
| grape | purple |
| banana | yellow |
+———–+———–+
4 rows in set (0.00 sec)
The ON clause replaced the WHERE clause, in this instance telling
MySQL to join together the rows in the tables where the IDs match
each other.
When joining tables using ON clauses, you can use any conditions
that you would use in a WHERE clause, including all the various
logical and arithmetic operators.
Another common type of JOIN is the LEFT JOIN. When joining two
tables with LEFT JOIN, all rows from the first table will be
returned, no matter if there are matches in the second table or not.
Suppose you have two tables in an address book, one called master_name,
containing basic records, and one called email, containing email records.
Any records in the email table would be tied to a particular id of a
record in the master_name table. For example
mysql> select name_id, firstname, lastname from master_name;
+———+———–+———-+
| name_id | firstname | lastname |
+———+———–+———-+
| 1 | John | Smith |
| 2 | Jane | Smith |
| 3 | Jimbo | Jones |
| 4 | Andy | Smith |
| 7 | Chris | Jones |
| 45 | Anna | Bell |
| 44 | Jimmy | Carr |
| 43 | Albert | Smith |
| 42 | John | Doe |
+———+———–+———-+
9 rows in set (0.00 sec)
mysql> select name_id, email from email;
+———+——————+
| name_id | email |
+———+——————+
| 42 | jdoe@yahoo.com |
| 45 | annabell@aol.com |
+———+——————+
2 rows in set (0.00 sec)
Using LEFT JOIN on these two tables, you can see that
if a value from the email table doesn’t exist, NULL will
appear in place of an email address:
mysql> select firstname, lastname, email fom master_name
left join email
-> on master_name.name_id = email.name_id;
+———–+———-+——————+
| firstname | lastname | email |
+———–+———-+——————+
| John | Smith | NULL |
| Jane | Smith | NULL |
| Jimbo | Jones | NULL |
| Andy | Smith | NULL |
| Chris | Jones | NULL |
| Anna | Bell | annabell@aol.com |
| Jimmy | Carr | NULL |
| Albert | Smith | NULL |
| John | Doe | jdoe@yahoo.com |
+———–+———-+——————+
9 rows in set (0.01 sec)
A RIGHT JOIN works like LEFT JOIN, but with the table order
reversed.
In other words, when using a RIGHT JOIN, all rows from the
second table will be returned,
no matter whether there are matches in the first table or not.
However, in the case of the master_name and email tables, there
are only two rows in the email table,
whereas there are nine rows in the master_name table. This means that
only two of the nine rows will be returned:
mysql> select firstname, lastname, email from master_name right join
email
-> on master_name.name_id = email.name_id;
+———–+———-+——————+
| firstname | lastname | email |
+———–+———-+——————+
| John | Doe | jdoe@yahoo.com |
| Anna | Bell | annabell@aol.com |
+——+—–+—————————-+
2 rows in set (0.00 sec)
Several different types of JOINs are available in MySQL, and
you’ve learned about the most common types.
You can also learn more about JOINs such as CROSS JOIN,
STRAIGHT JOIN and NATURAL JOIN