Solved

MySQL group on parent ids.

Posted on 2013-11-15
8
726 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 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Creating and Managing Databases with phpMyAdmin in cPanel.
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 …

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now