We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


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

theGhost_k8 asked
Medium Priority
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!!

Watch Question

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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Database Consultant
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.