Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Complicated tree query
Message
From
11/10/2006 06:13:20
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
10/10/2006 01:55:31
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01160753
Message ID:
01161129
Views:
13
>Hi, i have a table with two fields: id, parentid and name and with this i need to create a query where it shows the childs of the parents and so on. For instance
>
>ID PARENTID NAME
>3 LUCIA
>4 3 JOHN
>5 3 SANDRA
>6 4 CARLOS
>7 4 NANCY
>
>this should be displayed and reflected in the code according if i choose lucia or sandra (for instance)
>
>Thx.

Edgar,
This is not different from what Boris and Juri suggested.
* sample data
TEXT TO m.lcData noshow
3  LUCIA
4 3 JOHN
5 3 SANDRA
6 4 CARLOS
7 4 NANCY
ENDTEXT
STRTOFILE(m.lcData,'tmpdata.txt')

CREATE CURSOR mytree (ID i, PARENTID i, NAME c(10))
APPEND FROM tmpdata.txt TYPE DELIMITED WITH "" WITH blank
ERASE ('tmpdata.txt')
* sample data

SELECT t.Name, p.Name as parent, c.Name as child ;
	FROM myTree t ;
	LEFT JOIN myTree p ON p.ID = t.parentID ;
	LEFT JOIN myTRee c ON c.parentID = t.ID
However on the long run such structures need additional info like rootID (topmost id of a particular node), level (how deep in tree), position (order position of children within a particular parent node). Those additional info make it easy to place this info say into a treeview within order you want or encode/decode text data like this into a table and tree:
Lucia
Lucia\John
Lucia\John\Carlos
Lucia\John\Nancy
Lucia\Sandra
My .2 cents.

PS: Sometimes it's easy to process this complexity by placing this data into an activex Treeview and then traverse nodes getting their parent/siblings/children.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform