0

I'm building a task system in php/mysql, imagine I have the following structure:

id | parentid 
1    0
2    1
3    1
4    1
5    4

A parentid of 0 means there are no sub items.

So basically, I need to query all items that have a parentid of 1.

In a tree view example I'd get:

1
--2
--3
--4

But I also need to grab all the other subitems:

1
--2
--3
--4
---5

I hope this clears up what I need, this only goes into 3 levels, but I could very well have a huge number of levels.

How should I approach this in MYSQL? Getting a full list of all items with a specific parentid and ALL the other subitems?

Is this even a possible without knowing a limit? Maybe approach this via server side? (but then again, when to stop?)

Jorg Ancrath
  • 1,447
  • 10
  • 34
  • 61

1 Answers1

2

This question has already been answered. Best way to go about this is using recursive query which MySQL doesn't support.

Here is a workaround for it: Using MySQL query to traverse rows to make a recursive tree

You could do it in PHP or any other language you use for server side but it would take a lot of requests to get all the data you need.

Community
  • 1
  • 1
Martin Tale
  • 877
  • 1
  • 6
  • 18