Solved

MySQL group on parent ids.

Posted on 2013-11-15
8
713 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

707 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

14 Experts available now in Live!

Get 1:1 Help Now