Q&A

Q1:I have run a join but get many thousands of rows of data in my resultset, when each table only has a few hundred rows. Help!
A1: You probably didn't use the JOIN keyword, but listed several tables in your FROM clause. You may have done something like this:
SELECT * FROM table1, table2
								

Right? This is a common mistake. You performed a cross join when you meant to do a different type of join. You forgot to specify the relationship between the two tables.

The solution is to change your SQL to look like this:

SELECT * FROM table1, table2
  WHERE table1.id_col = table2.id_col
								

or to write your SQL using the JOIN keyword, like this:

SELECT *
  FROM table1
  INNER JOIN table2
  ON table1.id_col = table2.id_col
								
Q2:Can I join more than two tables?

Get Sams Teach Yourself MySQL in 21 Days, Second Edition 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.