Delete operations with unlimited classifications

Source: Internet
Author: User
Unlimited classification

The infinite classification table associated with the parent ID, if you want to delete one of them, how to delete all its descendants?
Previous moderators have written not recursive tree arrays, so here to see if there is no recursive method to find out all the descendants of the element can be used to delete


Reply to discussion (solution)

Without recursion, I won't. I hope the answers are given by the Daniel Downstairs.
In particular, I would like to ask a question: "Is there a need for real-world applications?" The project manager won't let you use recursion? ”
Not to solve the problem for the purpose of the trouble is bullying ... Ha ha...

Without recursion, I won't. I hope the answers are given by the Daniel Downstairs.
In particular, I would like to ask a question: "Is there a need for real-world applications?" The project manager won't let you use recursion? ”
Not to solve the problem for the purpose of the trouble is bullying ... Ha ha...

Of course there is no forced regulation, just to find out that there are other ways to achieve the purpose. It's like I didn't see it. Before the implementation process of the moderator, the construction of the tree array has always been thought to be recursive

Find Orphans
Select ID from table where PID not in (select ID from table)
Delete the corresponding record after

Need to be done in a loop, because each time a deletion is possible new orphans are created

Roughly like this:

do {  $rs = mysql_query (' Select-Group_concat (ID) from table where PID not-in (SELECT-ID from table) ');  List ($g) = Mysql_fetch_row ($rs);  mysql_query ("Delete from table where ID in (g)");} Where (Mysql_affected_rows ());

Find Orphans
Select ID from table where PID not in (select ID from table)
Delete the corresponding record after

Need to be done in a loop, because each time a deletion is possible new orphans are created

Roughly like this:

do {  $rs = mysql_query (' Select-Group_concat (ID) from table where PID not-in (SELECT-ID from table) ');  List ($g) = Mysql_fetch_row ($rs);  mysql_query ("Delete from table where ID in (g)");} Where (Mysql_affected_rows ());

This is also a way of thinking, but this is also done several times the query and delete operations, and there are sub-queries.

Left and right values + upper and lower values are not recursive son to find dad up and down the value of the ancestors to find the following values

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.