¿Cómo crear una relación padre-hijo en la consulta? [duplicar]

Posible duplicado:
Pregunta jerárquica de SQL

I have below records to maintain. It has parent child relation. I have one table and from that table i am going to build dynamic menu...So it has below type of records. Like a tree..One parent has two sub menu and two sub menu contains two or three menu...So how to manage it by inline query?

Aquí está la estructura de mi mesa


This will be data

    ID                ParentID
    1                      0
    2                      0
    3                      0
    4                      0
    5                      1
    6                      1
    7                      5
    8                      5
    9                      2

How to create select query to get parent child relation?? if anyone have any idea about this than please help me.

preguntado el 31 de julio de 12 a las 13:07

select * from my_table "gets" the parent-child relation. What do you want the result to look like and que has probado in order to achieve this? -

@Ben: Not like that dude...what i am doing right now, I am fetching all the data without put any relation. I am managing by my code but i want it from inline query..got it? -

Actually I did not get it. Can you please add an example to your question? -

@user968441, I don't know about Ben, but I don't get it. What do you expect to see? That table holds your parent-child relationship. What else are you trying to get? Give us an example. -

I think this is a hierarchical table, and hence solvable with Oracle's CONNECT BY PRIOR ... START WITH syntax. Which is already been well-covered in SO: stackoverflow.com/… -

1 Respuestas

tal vez esto

select t.id
  from table_name t
 start with t.ParentID = 0
connect by prior t.id = t.ParentID

Respondido 31 Jul 12, 14:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.