Solved

MySQL group on parent ids.

Posted on 2013-11-15
8
758 Views
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
0
Comment
Question by:Gary
[X]
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
8 Comments
 
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.
0
 
LVL 58

Author Comment

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

Accepted Solution

by:
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?
0
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
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.
0
 
LVL 58

Author Comment

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

Have a read of : http://support.experts-exchange.com/customer/portal/articles/1162518-using-off-site-content-on-experts-exchange
0
 
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.
0
 
LVL 58

Author Closing Comment

by:Gary
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.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 39653674
Ok, thanks.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

635 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