Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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:
mysql> select * from self;
+--------+--------+------+
| parent | name   | sid  |
+--------+--------+------+
|      2 | Graham |    1 |
|      9 | Norman |    2 |
|      3 | Lisa   |    4 |
|      7 | Ruth   |    3 |
|      3 | Pat    |    5 |
|      3 | Phil   |    6 |
|      3 | Brian  |    8 |
+--------+--------+------+
7 rows in set (0.00 sec)


Here's the problem with a join in the normal style:

Code:
mysql> select * from self join self on parent = sid;
ERROR 1066: Not unique table/alias: 'self'


and here's the solution:

Code:
mysql> select * from self as child join self as parent on parent.parent = child.sid;
+--------+--------+------+--------+--------+------+
| parent | name   | sid  | parent | name   | sid  |
+--------+--------+------+--------+--------+------+
|      9 | Norman |    2 |      2 | Graham |    1 |
|      7 | Ruth   |    3 |      3 | Lisa   |    4 |
|      7 | Ruth   |    3 |      3 | Pat    |    5 |
|      7 | Ruth   |    3 |      3 | Phil   |    6 |
|      7 | Ruth   |    3 |      3 | Brian  |    8 |
+--------+--------+------+--------+--------+------+
5 rows in set (0.01 sec)


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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho