Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > Table joins Wish List | Report Bug | About Me ]

9.9.1     Complex joins

This is NOT the latest copy of this book; click here for the latest version.

In the previous example we have a one-to-one relationship between our data - one golf centre ID matches precisely one golf centre name. However, things are rarely this neat in the real world - all too often a client has two account managers, or, on the other hand, an account manager might not have an account right now. How do you craft a query that means "Select all account managers and the names of their accounts, excluding the ones that have no accounts currently"? To do that you need to investigate the world of complex joins, which has a microcosmic jargon of its own including terms such as Cartesian product, left, right, and full inner join, and left, right, and full outer join - a bit too much for this book!

However, suffice to say that difference types of joins are important. Right outer joins, for example, allow you to select all rows from table A where a field matches rows from table B, as well as any unmatched rows from table B. Left outer joins are similar, with the difference of returning any unmatched rows from table A. Full outer joins are also similar, with the difference of returning any unmatched rows from table A or table B - see the pattern?

As I said, this is not really the place for such in-depth discussion of database querying - generally this level of detail is only covered in books specifically on databases, which pretty much confirms that only a very few need to know how to use it all!





<< 9.9 Table joins   9.10 Using temporary tables >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow