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:
Dempsey
Scripting Contest Winner
*****

Avatar
http://AdamDempsey.net

Posts: 2395
Reputation: 53
38 / Male / Flag
Joined: Jul 2003
O.P. Another SQL Question
Well this is kind of a follow-on from my other thread: Number Range Query  but it's a different problem now.

I am using the following SQL query:   
code:
SELECT * M1PNON IN (SELECT M1ON FROM M1P WHERE M1PNON LIKE '573.%')
Firstly it finds the records where M1PNON has the order number of 573 and returns a list of the matching M1ON numbers.
Then it finds all the records that have their M1PNON field as one of the list just retireved.

This is working fine, but I have just found out that I need to do it another level deep as well, but I'm confused as to how.  Anyone have any pointers?

I havent explained it very well, I'll post some sample data:

code:
M1P            -            M1PNON


1            -            573
2            -            573
3            -            1
4            -            1
5            -            2
6            -            5
7            -            5

records 1 and 2 are top level parents.

3 and 4 are child records of record 1 and 5 is a child of number 2.

The query i have above can currently retireve both the parents and the first children, but it won't return a childs child, such as 6 and 7 which are children of record 5.

So what do I need to add to the query to also make it retrieve the third level?  Is it another IN statement?  If so where exactly?


Thanks for any help   :)
SoundPacks   -   Scripts   -   Skins

that's not a bug, thats an unexpected feature
03-09-2006 05:21 PM
Profile E-Mail PM Web Find Quote Report
Adeptus
Senior Member
****


Posts: 732
Reputation: 40
Joined: Oct 2005
RE: Another SQL Question
Do you only want to include one more level (easy), or did you just use that for illustration and really want to recursively follow the parent-child relationship chain and get all child records there are, arbitrary number of levels deep (hard)?

Also, I am uncertain why you are using LIKE instead of =, if your data resembles the example.  Is there any reason for that?

03-09-2006 05:46 PM
Profile E-Mail PM Find Quote Report
Dempsey
Scripting Contest Winner
*****

Avatar
http://AdamDempsey.net

Posts: 2395
Reputation: 53
38 / Male / Flag
Joined: Jul 2003
O.P. RE: Another SQL Question
Yea its just one more level, i thought there was only ever going to be two, but I just found out its actually three.

Ah yea i used a bad example, I have to use LIKE as the actual data is 573.001, 573.002 etc etc
SoundPacks   -   Scripts   -   Skins

that's not a bug, thats an unexpected feature
03-09-2006 05:48 PM
Profile E-Mail PM Web Find Quote Report
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
Dempsey
Scripting Contest Winner
*****

Avatar
http://AdamDempsey.net

Posts: 2395
Reputation: 53
38 / Male / Flag
Joined: Jul 2003
O.P. RE: Another SQL Question
Thanks, that sounds like that would do exactly what I need, except I'm not sure if I can use it.

I am using these SQL queries in a data transfer program that connects to an IBM server, and I don't full full SQL access, just some fields to fill in:
[Image: attachment.php?pid=615869]
If I tick 'Enable group functions' then the 'Group By' and 'Having' fields become enable, but the 'Join By' is always disabled for some reason.

So using what you told me, I'm not sure if I can do it that way, any ideas?

EDIT:  I managed to do it another way, prob not very efficient or whatever, but it works :D.
code:
M1PNON IN (SELECT M1ON FROM M1P WHERE M1PNON IN (SELECT M1ON FROM M1P WHERE M1PNON LIKE '573.%'))
Thanks for the help :D

.png File Attachment: sql.PNG (13.43 KB)
This file has been downloaded 176 time(s).

This post was edited on 03-10-2006 at 09:23 AM by Dempsey.
SoundPacks   -   Scripts   -   Skins

that's not a bug, thats an unexpected feature
03-10-2006 09:14 AM
Profile E-Mail PM Web Find Quote Report
Adeptus
Senior Member
****


Posts: 732
Reputation: 40
Joined: Oct 2005
RE: Another SQL Question
Whatever works is good (and I hate these query setup dialogs)!

What does the "Details..." button do?  It could be many things, but I wouldn't be surprised if it let you edit SQL directly somewhere under there.  May be worth finding out, in case you need to use more advanced queries in your project in the future.

Other than that, the "Join by" box might become enabled if you specify columns from more than one table, or select more than one table somewhere before this dialog. Which might make self-joins a challenge, depending on how aware the dialog is of table aliasing...

03-10-2006 03:00 PM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


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