MySQL: String concat multiple rows into row's column

by Bobby Cannon July 07, 2010 07:34 PM

MySQL has the function group_concat that allows one to condense a set of rows into one single row by concatenating the list of values.

Code (MySql): Select StatementSELECT ( SELECT GROUP_CONCAT(name SEPARATOR " :: ") FROM `contentcategories` scg WHERE scg.id = cg.parent OR scg.id = cg.id ) AS xname FROM `contentcategories` cg ORDER BY xname


This is very useful when storing "Categories" or "Breadcrumbs". I needed the code for storing my content categories. I will list the example data.

ID Name Parent
1 Programming 0
2 Operating Systems 0
3 C# (C Sharp) 1
4 Vista 2

So what you end up getting is...

Operating Systems
Operating Systems :: Vista
Programming
Programming :: C# (C Sharp)

 

 

Tags:

Database

Month List