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 8289657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:33:32+00:00 2026-06-08T12:33:32+00:00

I am programming in PHP / MySQL / Javascript. I have a list of

  • 0

I am programming in PHP / MySQL / Javascript.
I have a list of parts which we want to link in a child / parent relationship with no limit on the amount of tiers.

When I am picking from a list of parts to add a child to a parent I limit the list of parts to exclude the parent itself, and any parts which are already children of that parent.

What I have discovered is that I also want to exclude the grandparents of the parent as otherwise we can get an incestuous relationship, which when I display the tree of parts will create an infinite loop.

Not only that but I cannot allow the child part to be a great grandparent of the parent or great great grandparent e.t.c.

Here is the SQL statement I use currently which I think could also be improved by using LEFT JOIN but I am not skillful enough with SQL at this point.

SELECT * 
FROM sch_part_general 
WHERE (sch_part_general.part_id <> $parentId) 
AND (sch_part_general.part_id NOT IN 
  (SELECT part_id FROM sch_part_mapping WHERE parent_id = $parentId)
)

sch_part_general is a multi column table with all the parts, with part_id as the primary key.
sch_part_mapping is a two column mapping table with part_id (child) || parent_id (parent).

Could someone point me in the right direction with the SQL query? I am not keen on using a while loop to create the SQL statement as I think this will be quite inefficient but it is the only way I have considered might work so far.

  • 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. Editorial Team
    Editorial Team
    2026-06-08T12:33:34+00:00Added an answer on June 8, 2026 at 12:33 pm

    MySQL doesn’t have much (if any) support for hierarchical queries. If you want to stick to what is called theAdjacency List Model, all you can do is add a JOIN for each level you like to include. Needless to say this doesn’t scale well.

    On the other hand, if you can alter your Database Schema, I would suggest implementing the Nested Set Model.

    A very good explantion of the Nested Set Model is presented in Mike Hillyer’s blog

    Limitations of the Adjacency List Model

    Working with the adjacency list model in pure SQL can be difficult at
    best. Before being able to see the full path of a category we have to
    know the level at which it resides.

    Nested Set Model

    the concept of nested sets in SQL has been around for over a decade,
    and there is a lot of additional information available in books and on
    the Internet. In my opinion the most comprehensive source of
    information on managing hierarchical information is a book called Joe
    Celko’s Trees and Hierarchies in SQL for Smarties, written by a very
    respected author in the field of advanced SQL, Joe Celko.

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

Sidebar

Related Questions

I am programming in PHP mysql. I have recently got into OOP programming. So
I have a small programming problem (php & javascript). On index.php page I'm printing
i'm programming a php/mysql web app which is kinda like a blogging platform where
I have started programming for sometime with PHP using MySQL as the database and
We have got loads of options for php + MySQL + Apache combo... Which
I've done a fair bit of mysql php programming but am pretty new to
I am new to php, mysql, azure and web programming in general, so excuse
Quick background: I'm programming in PHP, I have a domain model with a separate
I'm just starting PHP programming as you can tell. I want the results of
I have recently started programming in PHP. I am building a cart in PHP.

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.