473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Tree structure


I'm looking for some advice on how to design a set of tables to represent a
tree style table structure in MySQL.

I have a table that stores a set of items, with each item having a type. The
type of the item belongs to a tree structure of types.

e.g.

Type1 +---- Type2 ---- Type3 +---- Type4
| +---- Type5
|
+---- Type6 ---- Type7

Type8 +---- Type9
+---- Type10 +---- Type11
+---- Type12

I need to run queries on the item table to pull back all items that belong
to a type or any of it's child types

e.g.

where type = Type2 or type = Type3 or type = Type4 or type = Type5
or
where type = Type10 or type = Type11 or type = Type12

(I hope the diagram and psuedo code helps to explain what I'm trying to
achieve :-))

At the moment I store the structure of the type tree in an array within PHP
and build up a long where clause but someone hinted that there might be a
more efficient database approach to this - a cross reference table?
Unfortunately I didn't get a chance to discuss it further with him.

If anyone could suggest an approach or even point me in the general
direction of some good info on the concept I'd much appreciate it,

Steve
Jul 19 '05 #1
3 1970
Nevermind. I should have done some more searching first :-)

If anyone is trying to do the same thing then they might want to check out
this article:

http://www.sitepoint.com/article/1105

It has an excellent approach to it, starting from page 2.

Steve
"Steve" <me@removethisbit_stephenmcnabb.com> wrote in message
news:40**********************@news-text.dial.pipex.com...

I'm looking for some advice on how to design a set of tables to represent a tree style table structure in MySQL.

I have a table that stores a set of items, with each item having a type. The type of the item belongs to a tree structure of types.

e.g.

Type1 +---- Type2 ---- Type3 +---- Type4
| +---- Type5
|
+---- Type6 ---- Type7

Type8 +---- Type9
+---- Type10 +---- Type11
+---- Type12

I need to run queries on the item table to pull back all items that belong
to a type or any of it's child types

e.g.

where type = Type2 or type = Type3 or type = Type4 or type = Type5
or
where type = Type10 or type = Type11 or type = Type12

(I hope the diagram and psuedo code helps to explain what I'm trying to
achieve :-))

At the moment I store the structure of the type tree in an array within PHP and build up a long where clause but someone hinted that there might be a
more efficient database approach to this - a cross reference table?
Unfortunately I didn't get a chance to discuss it further with him.

If anyone could suggest an approach or even point me in the general
direction of some good info on the concept I'd much appreciate it,

Steve

Jul 19 '05 #2
Nevermind. I should have done some more searching first :-)

If anyone is trying to do the same thing then they might want to check out
this article:

http://www.sitepoint.com/article/1105

It has an excellent approach to it, starting from page 2.

Steve
"Steve" <me@removethisbit_stephenmcnabb.com> wrote in message
news:40**********************@news-text.dial.pipex.com...

I'm looking for some advice on how to design a set of tables to represent a tree style table structure in MySQL.

I have a table that stores a set of items, with each item having a type. The type of the item belongs to a tree structure of types.

e.g.

Type1 +---- Type2 ---- Type3 +---- Type4
| +---- Type5
|
+---- Type6 ---- Type7

Type8 +---- Type9
+---- Type10 +---- Type11
+---- Type12

I need to run queries on the item table to pull back all items that belong
to a type or any of it's child types

e.g.

where type = Type2 or type = Type3 or type = Type4 or type = Type5
or
where type = Type10 or type = Type11 or type = Type12

(I hope the diagram and psuedo code helps to explain what I'm trying to
achieve :-))

At the moment I store the structure of the type tree in an array within PHP and build up a long where clause but someone hinted that there might be a
more efficient database approach to this - a cross reference table?
Unfortunately I didn't get a chance to discuss it further with him.

If anyone could suggest an approach or even point me in the general
direction of some good info on the concept I'd much appreciate it,

Steve

Jul 19 '05 #3
Nevermind. I should have done some more searching first :-)

If anyone is trying to do the same thing then they might want to check out
this article:

http://www.sitepoint.com/article/1105

It has an excellent approach to it, starting from page 2.

Steve
"Steve" <me@removethisbit_stephenmcnabb.com> wrote in message
news:40**********************@news-text.dial.pipex.com...

I'm looking for some advice on how to design a set of tables to represent a tree style table structure in MySQL.

I have a table that stores a set of items, with each item having a type. The type of the item belongs to a tree structure of types.

e.g.

Type1 +---- Type2 ---- Type3 +---- Type4
| +---- Type5
|
+---- Type6 ---- Type7

Type8 +---- Type9
+---- Type10 +---- Type11
+---- Type12

I need to run queries on the item table to pull back all items that belong
to a type or any of it's child types

e.g.

where type = Type2 or type = Type3 or type = Type4 or type = Type5
or
where type = Type10 or type = Type11 or type = Type12

(I hope the diagram and psuedo code helps to explain what I'm trying to
achieve :-))

At the moment I store the structure of the type tree in an array within PHP and build up a long where clause but someone hinted that there might be a
more efficient database approach to this - a cross reference table?
Unfortunately I didn't get a chance to discuss it further with him.

If anyone could suggest an approach or even point me in the general
direction of some good info on the concept I'd much appreciate it,

Steve

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Johnson | last post by:
Been banging my head on this for two days now. Hope someone can help! My test program below is in the form of a single JSP, with a Node class build in. (All the coded needed to run is below.) ...
2
by: ragha | last post by:
Dear friends I am emulating thr tree structure mentioned in the article http://www.15seconds.com/issue/010921.htm I have succesfully created the folder structure for level 2 I need this...
1
by: googleo | last post by:
Hi, in my application I want to handle and store data in a hierarchic data structure. For example: persons who manage houses; houses have various numbers of floors; floors have various numbers...
1
by: Srihari | last post by:
I'm trying to develop a tree structure using javascript. The node values of the tree are generating from a mysql table depending on login. The tree structure contains 3 sub levels. I developed...
4
by: Stephan Tobies | last post by:
Hi everyone, I am looking for a good data structure that could be used to represent families of trees with shared sub-trees and copy-on-write semantics. On a very abstract level, I would like...
3
by: _DS | last post by:
Problem with mapping a directory tree to a tree control: It takes a while to recurse subdirs and map them to nodes. This is solved in some books I've seen (MacDonald, Albahari) by reading the...
1
by: David Hirschfield | last post by:
I've written a tree-like data structure that stores arbitrary python objects. The objective was for the tree structure to allow any number of children per node, and any number of root nodes...and...
5
by: hankypan1 | last post by:
Hi All, I need a tree data structure for my application. It is the non -cyclic simple tree where i can have any number of children node and each child can recursively become a sub tree like a...
8
by: =?ISO-8859-1?Q?m=E9choui?= | last post by:
Problem: - You have tree structure (XML-like) that you don't want to create 100% in memory, because it just takes too long (for instance, you need a http request to request the information from...
0
by: mac | last post by:
I found that with memory allocating techniques used nowadays (addresses alignment, eg. on 32bit machines) one can detect loops in a tree structure very fast, without using extra memory. This is due...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.