group_cat question

This is a discussion on group_cat question within the MySQL Database forums, part of the Database Forums category; say i got a table like this : cat_id, name,parent. with rows 1,"Shops",0 2,"Fashion&...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-07-2008
M. K.
 
Posts: n/a
Default group_cat question

say i got a table like this :
cat_id, name,parent. with rows
1,"Shops",0
2,"Fashion",1

is there a way to create a query that would produce me the following
result? 2,"Shops / Fashion"
Reply With Quote
  #2 (permalink)  
Old 03-07-2008
strawberry
 
Posts: n/a
Default Re: group_cat question

On 7 Mar, 12:46, "M. K." <dilb...@i-wanna.live-spam-free.volny.cz>
wrote:
> say i got a table like this :
> cat_id, name,parent. with rows
> 1,"Shops",0
> 2,"Fashion",1
>
> is there a way to create a query that would produce me the following
> result? 2,"Shops / Fashion"


Something like:

SELECT CONCAT(t2.name,' / ',t1.name) FROM a_table t1
LEFT JOIN a_table t2 ON t1.parent = t2.cat_id
WHERE t1.cat_id = 2;
Reply With Quote
  #3 (permalink)  
Old 03-08-2008
Rik Wasmus
 
Posts: n/a
Default Re: group_cat question

On Fri, 07 Mar 2008 18:03:13 +0100, strawberry <zac.carey@gmail.com> wrote:

> On 7 Mar, 12:46, "M. K." <dilb...@i-wanna.live-spam-free.volny.cz>
> wrote:
>> say i got a table like this :
>> cat_id, name,parent. with rows
>> 1,"Shops",0
>> 2,"Fashion",1
>>
>> is there a way to create a query that would produce me the following
>> result? 2,"Shops / Fashion"

>
> Something like:
>
> SELECT CONCAT(t2.name,' / ',t1.name) FROM a_table t1
> LEFT JOIN a_table t2 ON t1.parent = t2.cat_id
> WHERE t1.cat_id = 2;


Indeed, with a join for every possible level in hierarchy (in which case
is CONCAT_WS() is more appropriate, because one could as easily concat a
7th level node as easily as a 2th level node without having problems with
an 'overhead' of '/'s).

If the level should be / is desired to be unlimited, and the select's are
much more abundant then alterations, the nested set model could be a
solution. Harder on the inserts/updates (in hierarchy/position)/deletes,
easier on the selects. See:
<http://dev.mysql.com/tech-resources/articles/hierarchical-data.html>
--
Rik Wasmus
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:03 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0