Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL group on parent ids.

Posted on 2013-11-15
8
Medium Priority
?
784 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
  • 5
  • 3
8 Comments
 
LVL 30

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 30

Accepted Solution

by:
Olaf Doschke earned 2000 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 30

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 30

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 30

Expert Comment

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

916 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