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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:34:05+00:00 2026-06-09T15:34:05+00:00

I have got a table structure in mysql like this: ________________________ | id |

  • 0

I have got a table structure in mysql like this:

  ________________________
 |  id      | int(auto)   |     
 |------------------------|   
 |  name    | varchar     |
 |------------------------|
 |  link    | varchar     |
 |------------------------|
 | parent_id| int         |
 |________________________|

Basically it is a design for a database driven menu, the root menu has parent_id 0, whereas child menu have their root menu specified on parent_id field. It can be up to 3-4 level.

Now, I need to find all child menu under a root menu. I can easily do that for one level menu, but I can’t traverse behind that. I can do that by writing a program (by merging resultsets), but it will be much easier and convenient way, if I can do that using just SQL.

I can’t change any structure of the table or otherwise modify the system. Please 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. Editorial Team
    Editorial Team
    2026-06-09T15:34:06+00:00Added an answer on June 9, 2026 at 3:34 pm

    I have used the following query to get all the child menu id (comma separated) of a root menu (in this query it is 2). It can support upto 3 levels. I know the query is not optimized and got performance issue, but still I think it is the only way to achieve the solution, without using procedure(which I can’t use).

    select group_concat(lev0) from 
    (select concat_ws(',',lev2,group_concat(lev3)) as lev0 from 
    (SELECT  t2.id as lev2, 
    t3.id as lev3, 
    t4.id as lev4
    FROM menus AS t1
    LEFT JOIN menus AS t2 ON t2.parent_id = t1.id
    LEFT JOIN menus AS t3 ON t3.parent_id = t2.id
    LEFT JOIN menus AS t4 ON t4.parent_id = t3.id
    WHERE t1.id = '2') t0 group by lev2) t;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a really simple table structure like this: Table Page Hits id |
I have a table structure looking something like this. One outer table and in
I have got a simple MySQL table and primary index (id) is not numbered
I have a looklup table, its got an ID and a NAME field. The
i have got a problem with my SQL Statement: Table structure: CREATE TABLE tags
I have got a table structure which is as follows: `CREATE TABLE "pages" (
I've got a MySQL table with, among other things, two fields: a (int) and
I am learning to use mysql.So, I got some problems. I have three table
I got this table structure where we are not allowed to change and i
Table structure: users: uid, name friendships: uid1, uid2 game_membership: gid, uid basically I'm trying

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.