| |||||||||||
Joining a table to itself Posted by enquirer (enquirer), 8 December 2005 I have a table that I wish to join to itself, but I get all sorts of problems when I attempt to do the join - messages about ambiguous fields, inaccurante results, etc. Using AND and OR just added extra results.[heavily paraphrased from original question] Posted by admin (Graham Ellis), 8 December 2005 You're going to have all sorts of issues like this if you don't find some way to resolve the issue of ambiguous column names - and you can do that using an AS clause which is a way of aliasing (adding and extra name) to elements of your SELECT.Let me give you an example. I have a table of people - parent and offspring are all people, so they're all in the same table. I have a column for the person's ID and also another column for the parent's ID and I wish to join up the families. Code:
Here's the problem with a join in the normal style: Code:
and here's the solution: Code:
By giving each of the two incoming copies of the "self" table their own unique name (alias), our join condition can be uniquely specified thus overcoming the initial problem reported. Result - a correct set of responses. This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |