Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 162795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:27:16+00:00 2026-05-11T11:27:16+00:00

I have a list of items in MySQL, connected through a column parent_id. Let’s

  • 0

I have a list of items in MySQL, connected through a column ‘parent_id’.

Let’s assume the columns are: id, name, parent_id

If one of my users deletes an item high in the hierarchy, I need to delete all of its children. So, two part question:

1) Is there an effective and efficient MySQL call that will return ID’s for all items where its parent no longer exists?

2) In PHP, I’m thinking I can get these orphaned ID’s from MySQL into an array, then run a foreach loop and delete each?

Much thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:27:16+00:00Added an answer on May 11, 2026 at 11:27 am

    I guess what the original author had in mind was something in the lines of hierarchical queries.

    Unfortunately, MySQL does not have native support for hierarchical queries (unlike, for example Oracle, where you could use CONNECT BY to achieve what you want).

    Probably the easiest way to remove all orphans would be to execute a query like:

       SELECT t1.id      FROM table t1       LEFT JOIN table t2 ON t2.id = t1.parent_id     WHERE t2.id IS NULL 

    This would yield you all rows from table where their parent doesn’t exist.

    Pair this with a PHP script that keeps executing the query and deleting any results, and after a couple of iterations your table should be free of orphans (this whole thing can probably be merged into one DELETE statement you could just execute in a while loop).

    You need to execute the select-delete multiple times because of transitivity — consider a situation where an orphan is parent to another record; with the first iteration you’d remove the first orphan, making the next record in chain an orphan.

    Also, make sure you explicitly skip the head of your hierarchy, otherwise you’ll end up with an empty table (as the head is, by definition, an orphan).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.