parent child - same table - list all / below in hirarachy

Posted on 2009-02-10
Last Modified: 2012-05-06
table structure:

id   pid
1   0
2   0
3   0
4   1
5   2
6   3
7   4
8   4
9   3

Relations/ hierarchy :--
(hierarchy may deepen to level four as well..)

I want to delete all records from that level and blow the hierarchy.
in case of 4, query should be: (obtain 4 and sub-sub-child of 4)
    -- delete from table-name where id in (4,7,8);
in case of 3, query should be giving: (3,child and sub-child....)
    --> delete from table-name where id in (3,6,9); ####
in case of 9 it should only delete record id=9.

I'm trying this with single query - self join but its difficult to obtain as level of hierarchy may differ.

Can any one provide me with suitable solution!!

Question by:theGhost_k8
    LVL 10

    Assisted Solution

    As level of hierarchy may differ you can't do it with a single query. You have to use a loop in mysql function or any programming language.
    LVL 21

    Accepted Solution

    i got that by query its not psble, i tried cursor but it seems for higher number of data, it will slow down the performance. i'll go for procedure/function.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now