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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:49:42+00:00 2026-06-03T14:49:42+00:00

Consider the database table: folders folders id parent_id name 1 0 a 2 1

  • 0

Consider the database table: folders

folders
id   parent_id    name
1    0            a  
2    1            b  
3    2            c

‘b’ is a folder inside ‘a’ and thus it’s parent_id is the id of ‘a’.
Folders with a parent_id of 0 simply means it’s in the root folder.

I wrote a recursive function in php that helps me get the ID of a path that I’m interested in.

For example:

echo get_folder_id('a/b/c'); // 3 (3 SQL queries)  
echo get_folder_id('a'); // 1 (1 SQL query)  
echo get_folder_id('a/b'); // 2 (2 SQL queries)  
echo get_folder_id('a/c'); // false (2 SQL queries)


Problem: For each folder in the path, I had to make a SQL query to the DB server.

Question: Is there a way I can reduce the number of queries if the path is ‘a/b/c’?

Here’s my current working solution for reference.

function get_folder_id($path, $parent_id=0) {
    $path = explode('/', $path);
    $id = 0;

    //if there's only 1 folder in the path, query the database for the ID
    if (count($path) == 1) {
        $rs = $this->db->select('id')
                ->from('folders')
                ->where('name', $path[0])
                ->where('parent_id', $parent_id)
                ->limit(1)
                ->get();

        if ($rs->num_rows() == 0) return FALSE;

        $result = $rs->row_array();
        return $result['id'];
    }

    foreach($path AS $i=>$p) {
        if ($i==0 && $p=='') continue; //if a path starts with / move on to the next item
        $id = $this->get_folder_id($p, $id);
    }

    return $id;
}

Note: I’m using CodeIgniter framework but this isn’t related to my question. Just for folks wondering why I used $this->get_folder_id() and $this->db

  • 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-03T14:49:44+00:00Added an answer on June 3, 2026 at 2:49 pm

    One solution I just came up is to use a series of LEFT JOINs on the same table.

    SELECT tb3.id FROM folders AS tb1
    LEFT JOIN folders AS tb2 ON tb2.parent_id=tb1.id
    LEFT JOIN folders AS tb3 ON tb3.parent_id=tb2.id
    WHERE tb1.name='a' AND tb2.name='b' AND tb3.name='c'
    LIMIT 1
    

    Based on the number of folders in the path, I can dynamically generate the above SQL.
    I’m not sure if this is the perfect solution, but right now it’s working as intended.

    Any feedback on whether this is a good idea or not is greatly appreciated!

    $path = 'a/b/c';
    $path = explode('/', $path);
    
    $sql = "SELECT tb".count($path).".id FROM folders AS tb1";
    for($x=1, $max = count($path); $x<=$max; $x++) {
        $sql .= " LEFT JOIN folders AS tb".($i+2);
        $sql .= " ON tb".($i+2).".parent_id = tb"($i+1).".id";
    }
    
    $sql .= " WHERE 1";
    
    foreach($path AS $x => $p) {
        $sql .= " AND tb".($x=1).".name='".$p."'";
    }
    
    $sql .= " LIMIT 1";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the below situation I have a database table as below user_id user_rank ------------------
Consider the following database tables: Table messages with 13,000,000 rows (one row per message).
In setting up a MySQL database, consider that I have a table of products
I need to create a table in MS access database. Consider, 'ConfigStructure.mdb' being my
In a database consider that I have a table with 4000 rows. I am
Consider a database table holding names, with three rows: Peter Paul Mary Is there
I'm creating a database for a dictionary. Consider the table WORD. My current plan
We're designing a database in which I need to consider some FK(foreign key) constraints.
Let's consider this fabfile def syncdb(): print(green(Database Synchronization ...)) with cd('/var/www/project'): sudo('python manage.py syncdb',
Would you consider it bad practice to store various user data in a database

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.