MySQL group on parent ids.

Posted on 2013-11-15
Last Modified: 2013-11-16
Say I have this table layout
id	name	parent
1	cat 1	0
2	cat 2	1
3	cat 3	2
4	cat 4	3
5	cat 5	0
6	cat 6	5
7	cat 7	6

Open in new window

Is it possible with one request to get that group of 4 categories and the group of 3 individually concatenated and so on for each grouping .  So I end up with this in my recordset
cat 1/cat 2/cat 3/cat 4
But also get
cat 1
cat 1/cat 2
cat 1/cat 2/cat 3

Top level parent is always 0
Question by:Gary
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 29

Expert Comment

by:Olaf Doschke
ID: 39653614
What you typically d is query all child nodes for one parent and then iterate the child nodes to redo the query with the child ids as parent ids.

Something like

SELECT id, name from table where parentid=?

Binding the ? to a integer variable holding the current parent id, inited with 0, you can reuse that query for any level.

Also typical, you begin with just the root level and let user interaction of expanding root nodes expand further sub records.

Would you like to see more sample code? Do you know how to bind variables to queries?

Bye, Olaf.
LVL 58

Author Comment

ID: 39653631
I need all possible combinations in one go.
LVL 29

Accepted Solution

Olaf Doschke earned 500 total points
ID: 39653650
<competing site link removed - GaryC123>

It's easy to do one level, but not all in one go. But there's nothing wrong with recursing doing several queries.

To make this faster you might also simply load all data into PHP objects and then recurse the result to build a tree in memory.

Or let me ask the counter question: What's the purpose to have all the concatenations of categories in rows? To display a tree? A treeview control or widget is there to display a tree, and guess what you feed it? Root nodes first, then child nodes. For example this is done with an Nodes.Add(value,nodekey,[parentnodekey]) method with an optional parent key. And that means? You feed in the data in the form you have using the id as nodekey, and parentid as parentnodekey, and that's it. For that matter you even only need SELECT * FROM table ORDER BY parentid,id

Bye, Olaf.

Edit: Of course that won't help if you don't plan to drive a treeview with your data. Give me more prospect of what you try to achieve. Recursive SQL is not part of mysql. But you can do the recursion in PHP. Important is to solve the problem, not to push it into one query, isn't it?
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

LVL 29

Expert Comment

by:Olaf Doschke
ID: 39653658
Sorry, Gary. Is it really against forum rules to point to other discussion, just because it's elsewhere? This really is limiting discussion.

Bye, Olaf.
LVL 58

Author Comment

ID: 39653663
Yes it is especially when that link only serves to provide other links.

Have a read of :
LVL 29

Expert Comment

by:Olaf Doschke
ID: 39653670
I've done this often enough and links weren't remove, but OK.

So back to the question. What is your goal?

Bye, Olaf.
LVL 58

Author Closing Comment

ID: 39653673
Since posting this yesterday I changed my DB design to make this easier so the question is moot.  If I hadn't forgotten about it I would have deleted it.
But the accepted answer provide a means and a way to accomplish it.
LVL 29

Expert Comment

by:Olaf Doschke
ID: 39653674
Ok, thanks.

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question