group_cat question

  • Follow


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"
0
Reply M 3/7/2008 12:46:33 PM

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;
0
Reply strawberry 3/7/2008 5:03:13 PM


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

> 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 =3D t2.cat_id
> WHERE t1.cat_id =3D 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 wit=
h  =

an 'overhead' of '/'s).

If the level should be / is desired to be unlimited, and the select's ar=
e  =

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
0
Reply Rik 3/8/2008 1:42:36 AM

2 Replies
215 Views

(page loaded in 0.032 seconds)

Similiar Articles:






7/26/2012 11:48:14 AM


Reply: