What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Another SQL Question

Another SQL Question
Author: Message:
Adeptus
Senior Member
****


Posts: 732
Reputation: 40
Joined: Oct 2005
RE: Another SQL Question
Ok then. 

Although I was the one to suggest subqueries to you, my choice for what you already are doing would be:

code:
SELECT Child.* FROM M1P AS Child
    JOIN M1P AS Parent ON Child.M1PNON = Parent.M1ON
    WHERE Parent.M1PNON LIKE '573.%'


This is a self join -- where you join the same table to itself -- and as such, you have to alias your table names.  Although most people use very short aliases, I chose descriptive longer ones here, to illustrate what's going on.

This does exactly what your query does, but is a lot more readable.  It is also very easy to have it dig one level deeper: you just add another self join:

code:
SELECT Child.* FROM M1P AS Child
    JOIN M1P AS Parent1 ON Child.M1PNON = Parent1.M1ON
    JOIN M1P AS Parent2 ON Parent1.M1PNON = Parent2.M1ON
    WHERE Parent2.M1PNON LIKE '573.%'


This would get you the next level of child rows.  If you want both levels returned in the same resultset, just UNION ALL the two together:

code:
SELECT Child.* FROM M1P AS Child
    JOIN M1P AS Parent ON Child.M1PNON = Parent.M1ON
    WHERE Parent.M1PNON LIKE '573.%'
UNION ALL SELECT Child.* FROM M1P AS Child
    JOIN M1P AS Parent1 ON Child.M1PNON = Parent1.M1ON
    JOIN M1P AS Parent2 ON Parent1.M1PNON = Parent2.M1ON
    WHERE Parent2.M1PNON LIKE '573.%'


Hope that helps.  :)
03-09-2006 06:31 PM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Another SQL Question - by Dempsey on 03-09-2006 at 05:21 PM
RE: Another SQL Question - by Adeptus on 03-09-2006 at 05:46 PM
RE: Another SQL Question - by Dempsey on 03-09-2006 at 05:48 PM
RE: Another SQL Question - by Adeptus on 03-09-2006 at 06:31 PM
RE: Another SQL Question - by Dempsey on 03-10-2006 at 09:14 AM
RE: Another SQL Question - by Adeptus on 03-10-2006 at 03:00 PM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On