Solved

Concat based on parent id

Posted on 2014-02-03
9
322 Views
Last Modified: 2014-02-06
Say I have this
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


I want to select all rows (or rather store these ID's in each row of the table), but for each row concat the ID's of the parent hierarchy.
So say for cat 3 I would end up with
cat 3     1,2,3

...where 1, 2 and 3 are the ID's of the row itself, the ID of its parent and the ID of the parent. There is never more than 3 levels and the order doesn't matter.
0
Comment
Question by:Gary
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39831210
I would suggest that you add a fourth field to store that in so you don't mess up your ability to trace thru your hierarchy.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39831222
Gary: I am not sure this helps, but I've seen a similar/different question recently.  Please have a look:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28336520.html
0
 
LVL 58

Author Comment

by:Gary
ID: 39831247
Maybe but that is array based, I'm looking at an sql statement I can run one time on the database.
If nobody else comes along I'll look through it again, bit too much to digest right now.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39831468
You could use Common table Expressions (CTE). Here is a link to the example

http://technet.microsoft.com/en-us/library/ms186243(v=sql.105).aspx
0
 
LVL 58

Author Comment

by:Gary
ID: 39836851
What has MSSQL got to do with it?
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39839005
Is nt it in SQL you are trying to achieve this?
0
 
LVL 58

Author Comment

by:Gary
ID: 39839212
Yes, but what you can do in MSSQL is not the same as what you an do in MySQL
0
 
LVL 58

Author Closing Comment

by:Gary
ID: 39839220
Ended up just running a couple of queries to build it as it is only a one off exercise, but the linked to Q has all the logic for doing it.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39839736
Thanks, Gary!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

740 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