Main Content

Relating tables with joins in MySQL

Archive - Originally posted on "The Horse's Mouth" - 2010-02-21 07:53:33 - Graham Ellis

Relational Databases are stores of data in which that data can be matched using common characteristics. For example, I have a table of data about programming languages (which includes a columns for the name of the language and the author) and another table of data about our courses which includes the course name, and the level - beginner, intermediate or advanced. With a relational database, one of the ways I can link the tables (relate the data in them together) is via a join.

Aside - It's a good idea to keep data in separate tables as I've described in my example to avoid having to duplicate the same information many times over - that would be inefficient, and a maintenance nightmare
.

When you are joining tables, you tell MySQL which column in the first table is to link to which column in the second table. And you also need to decide what to do about partial records that don't match up - do you ignore them, or fill them in with nulls to make the record complete? We have a very popular tutorial [here] on this.

If you are looking for a combined table for presentation which includes unmatched ("orpan") records in both directions at the same time, you'll not be able to produce it with a single select - but rather you'll use a UNION of two selects, which you can combine into a single SQL statement. Here is an example:

(select * from dm_subjects left join dm_courses
on su_id = co_su)
union
(select * from dm_subjects right join dm_courses
on su_id = co_su where su_id is NULL)
order by su_name desc, co_key;


The first join selects all the records that match across the tables and the records which are orphans in the left table. The second join adds in all the records which are built from orphans in the right table.

In my example, I can now produce a combined result which shows on a single display ALL of my courses, ALL of the languages, whether of not there is a language description for the course, and whether or not we offer a course in the language.

Let's see that table:




You'll note numerous complete records ... and also records for the Go! and Scala programming languages on which (at the time of writing) we don't offer courses. And you'll see records for our Linux and MySQL courses, even though those are not really programming languages to don't have the extra data from the language table.

Aside - Note the difference between a NULL field and an empty one - in the example I have put an empty field against Java to demonstrate the different handling. This is another vital aside - you MUST not assume that just because you don't know how many children someone has that they have none, and in effect that's what you're doing if you confuse NULL and "".


The complete set of MySQL instructions that I used to set up this examples may be seen [here] (also includes sample joins!) and the PHP program / page which I used to present the data on the user's browser is [here]. You can run the join demonstration for yourself on our server [here] ... and of course we would welcome you on our MySQL Course here.