0

I've following tasks. t1, t2, t3, t4, t5, and t6. There can be any number of such tasks. I've taken 6 tasks just for the example.

Each task may depend on some parent tasks, a task cannot have more than 1 parent task. Conversely, each task may have one or more dependent tasks.

Let's consider following example:

    parent-task    child-task
        t5             t4
        t6             t5
        t1             t6
        t2             t3
        t4             t2

Children task hierarchy of t5: t4 <-- t2 <-- t3
Children task hierarchy of t6: t5 <-- t4 <-- t2 <-- t3
Children task hierarchy of t1: t6 <-- t5 <-- t4 <-- t2 <-- t3
Children task hierarchy of t2: t3
Children task hierarchy of t4: t2 <-- t3
Children task hierarchy of t3: empty set 

parent task hierarchy of t4: t5 --> t6 --> t1
parent task hierarchy of t5: t6 --> t1
parent task hierarchy of t6: t1
parent task hierarchy of t1: empty set
parent task hierarchy of t3: t2 --> t4 --> t5 --> t6 --> t1
parent task hierarchy of t2: t4 --> t5 --> t6 --> t1

I'm not being able to figure out query that can give me hierarchy of parent and children tasks of each task. Stored procedures aren't allowed. So, I can't write a stored procedure.

sameer.oak
  • 13
  • 2
  • is [this](https://stackoverflow.com/questions/20215744/how-to-create-a-mysql-hierarchical-recursive-query) helpful for you? – Prateik Darji Feb 21 '20 at 08:49
  • Perfect. the only glitch is it'sn't giving me the last child. in the current problem context, children tasks hierarchy of 1 is 6 -> 5 -> 4 -> 2 -> 3. The query is giving only 6 and 5. here's the changed query: `select child_task_idfk from (select * from parentchild_tbl order by parent_task_idfk) parents, (select @pv := 1) initialisation where find_in_set(parent_task_idfk, @pv) and length(@pv := concat(@pv, ',', child_task_idfk));` – sameer.oak Feb 21 '20 at 09:38
  • so its working for you now right? – Prateik Darji Feb 21 '20 at 10:16
  • Does this answer your question? [How to create a MySQL hierarchical recursive query](https://stackoverflow.com/questions/20215744/how-to-create-a-mysql-hierarchical-recursive-query) – Prateik Darji Feb 21 '20 at 11:27

0 Answers0