SunQuest
 
           MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old August 30th, 2005, 05:35 AM
pawelsm pawelsm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: muenchen
Posts: 5 pawelsm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 14 m 49 sec
Reputation Power: 0
searching the tree

Is there any easy way to retrieve - by SELECT and LEFT JOIN statements - all (not only immediate) branches starting from the chosen node?

Sincerely,

Pawel

Reply With Quote
  #2  
Old August 30th, 2005, 07:25 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
What does your table structure look like?

Reply With Quote
  #3  
Old August 30th, 2005, 07:59 AM
pawelsm pawelsm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: muenchen
Posts: 5 pawelsm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 14 m 49 sec
Reputation Power: 0
Quote:
Originally Posted by MadCowDzz
What does your table structure look like?


just one table with node inf. like:

id; parent_id; value; depth(how far from root)

Now - each LEFT JOIN ON will relate one level of the tree.

I'm not a db specialist so maybe my question is totally out of blue but here it is:

Would it be possible using SQL statements to retrieve all nodes which are downstream from given one?

Sincerely,

Pawel

Reply With Quote
  #4  
Old August 31st, 2005, 05:30 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 3
It is possible with two disadvantages:
1. using one table multiple times within a query is very slow
2. the number of branches is always limited to your query. the more branches you want to have max defines the difficulty of your query and makes it slower.

It is something like

SELECT l1.id, l2.id, l3.id
FROM table l1
LEFT JOIN table l2 on l2.parent = l1.id
LEFT JOIN table l3 on l3.parent = l2.id
WHERE l1.parent=$given

Expand this as you like. Maybe with something like CONCAT(l1.id, l2.id, l3.id) as id to get only one field ... if this would work.

I would greatly advice you to make a function in you script that is called for every branch. A recursive function is the ideal solution here.
In my experience this is easier, more expandable and faster.

But if anyone has another conclusion, I'm (very) happy to read it ;-)
Comments on this post
pawelsm agrees!

Reply With Quote
  #5  
Old September 1st, 2005, 03:05 AM
pawelsm pawelsm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: muenchen
Posts: 5 pawelsm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 14 m 49 sec
Reputation Power: 0
I was planning to get over this by introducing two fields:
1) depth - INT describing distance from the root
2) lineage - STRING which child is taking from parent and concat it with its own name.
this is idea from one of tutorials but I was hopping that there is some easier was which do not suffer from n+1 selection problem.

Thanks for your help,

Sincerely,

Pawel

Reply With Quote
  #6  
Old September 1st, 2005, 04:57 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 3
Indeed, if you have a field in your database containing all IDs of its parents (like "4;12;16;45") you can indeed simplify the query.

Then it is something like:

SELECT id FROM table WHERE parents like '%$given_parent%' ORDER BY parents

But if this is better/quicker/more expandable then the script solution is all a matter of tree-complexity. When you have a site menu structure a script is better, but when you have a database with all people from a country and their relationships (parent/child e.g.) then the linked-parent-field will probably work better

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > searching the tree


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway