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

  • Home
  • SEARCH
  • 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 6602185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:50:49+00:00 2026-05-25T18:50:49+00:00

I have a common database structure using parent_id for a hierarchy. I’m trying to

  • 0

I have a common database structure using parent_id for a hierarchy.

I’m trying to find out the depth of an item.

So take for example this:

ID  Name    Parent_id
1   Games   0
2   Nintendo    1
3   DS  2
4   3D  3

If I wanted to find out the depth of ID 4 (3D), the answer is 3. Any idea how I would query this or a combination of sql and php?

Thank you!

  • 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-05-25T18:50:49+00:00Added an answer on May 25, 2026 at 6:50 pm

    Do you have a naive implementation? are you looking for the best way? Trees are recursive, so I think you will query the db as much as the tree height.

    this pseudo-php

    function getHeigth($item_name) {
        $res = 0;
        $current_parent_id = executeSql("SELECT parent_id FROM games g WHERE g.name= ? " , $item_name);
        while ($current_parent_id != 0) {
            $current_parent_id = executeSql("SELECT parent_id FROM games g WHERE g.id = ? " , $current_parent_id);
            $res = $res + 1;
        }
        return $res;
    }
    

    This algorithm will perform bad if your three is not balanced.

    Storing depth would improve performance, but will impact in UPDATE and INSERT queries.

    Also if your tree is broken, this Psuedo-PHP could loop forever

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

Sidebar

Related Questions

We have a common problem of moving our development SQL 2005 database onto shared
I have a database where one of the common queries is has a where
I have what appears to be a fairly common scenario: I have a database
I have to choose the structure of a database that will store content types
The common normalized structure of database for a forum is to store all posts
I have a few classes, such as those that outline database table structure or
I have two VC++ 6 apps writing/updating data in a common Access 2000 database
I have unstructured geneally unclean data in a database field. There are common structures
I have common functionality that I need to access from all screens of my
I want to create templates for base new reports on to have common designs.

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.