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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:30:30+00:00 2026-06-06T04:30:30+00:00

I am building a navigation menu from a list of pages. The table is

  • 0

I am building a navigation menu from a list of pages.
The table is like this:

Table name: pages

id | type | parent | name
-------------------------------
1,    1,     null,   root1
2,    1,     null,   root2
3,    2,     2,      home
4,    2,     3,      child
5,    2,     4,      sub_child
6,    3,     5,      sub_sub_child


type:
1 = root page / site
2 = page
3 = ...

My problem is that from any page, I have to find the root page.
I have a column parent that refers to the parent page, except for the root pages.

I can have multiple root pages in the table, but each page has only one parent.

Could somebody help me to write a recursive query ?

I’m trying to use this query, but it doesn’t work:

with recursive pages (id, parent) as 
(
    select pages.id, 
    pages.parent, 
    from pages
    where pages.id = 4

union all
    select pages.id, 
    pages.parent,
    from pages
    inner join pages p on p.id = pages.parent
)
select id
from pages;

Thanks

  • 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-06T04:30:33+00:00Added an answer on June 6, 2026 at 4:30 am

    My farovite trick to handle tree structured data in database is add a column FullID to table to avoid complex (parhaps recursive) SQLs/Stored Procedures.

    FullID     id  parent   name
    -----------------------------
    1          1   null     root1
    2          2   null     root2
    2.3        3   2        home
    2.3.4      4   3        child
    2.3.4.5    5   4        sub_child
    2.3.4.5.6  6   5        sub_sub_child
    

    So, to find the root page id, just extract the first part of FullID via SQL or your application language.

    If using SQL, you can use the following SQL to get the root id.

    -- MySQL dialect
    select substring_index(FullID,'.',1) as RootID from table;
    
    -- SQL Server dialect
    select case charindex('.', FullID) when 0 then FullID else substring(FullID, 1, charindex('.', FullID)-1) end as RootID from table
    

    To delete a node and it’s children

    DELETE table WHERE id=<CURRENT_NODE_ID> OR FullID LIKE '<CURREN_NODE_FULLID>.%'
    

    To move a node and it’s children

    -- change the parent of current node:
    UPDATE table
    SET parent=<NEW_PARENT_ID>
    WHERE id=<CURRENT_NODE_ID>
    
    -- update it's FullID and all children's FullID:
    UPDATE table
    SET FullID=REPLACE(FullID,<CURRENT_NODE_PARENT_FULLID>, <NEW_PARENT_FULLID>)
    WHERE (id=<CURRENT_NODE_ID> OR FullID LIKE '<CURRENT_NODE_FULLID>.%')
    

    Note

    This trick is only applied on limited tree level cases, or the FullID can’t hold long content if tree level is too deep.

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

Sidebar

Related Questions

I am building navigation like this: http://jsfiddle.net/techydude/XZLDq/ I had help from other members to
I am building a website that remembers the users choices from a navigation menu
I am building a custom navigation menu in Magento which displays the categories from
I'm building a navigation system using jquery scrollto. I have my navigation menu in
I'm building a WPF app using pages and the navigation service. One of the
I'm building a navigation menu in my django app, and one of the options
So I'm building a responsive navigation menu that's 'fixed' to the top, it's 'bootstrap'
I'm building a website, and would like to make aa navigation bar with submenus.
I'm building a WPF navigation app, with pages. I want to ask the user
I am building a WPF app using navigation style pages and not windows. I

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.