Unix Technical Forum

Brain Not Working: Summary Fields

This is a discussion on Brain Not Working: Summary Fields within the MySQL forums, part of the Database Server Software category; --> I would like to create an sql select that listed all group records as a concatenated string, but I ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-13-2008, 06:14 PM
mcl
 
Posts: n/a
Default Brain Not Working: Summary Fields

I would like to create an sql select that listed all group records as
a concatenated string, but I just can not work out how to do it.

eg Table1 Clothes Colours
COLOUR
red
blue
green
yellow

Table2 Clothes
COLOUR ITEM
red pullover
green jumper
green trousers
yellow shirt
red hat
red scarf
red coat

I want to be able to create a Select with two fields COLOUR, ITEMs,
which does the following, where ITEMS is concatenated from each
matching group record in Table2 by COLOUR
COLOUR ITEMS
red pullover: hat: scarf: coat
green jumper: trousers
yellow shirt


I hope I have explained it correctly.

Any help or pointers, would be most appreciated, even if it is - NOT
POSSIBLE

Thanks

Richard
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-16-2008, 01:40 PM
Luuk
 
Posts: n/a
Default Re: Brain Not Working: Summary Fields

mcl schreef:
> I would like to create an sql select that listed all group records as
> a concatenated string, but I just can not work out how to do it.
>
> eg Table1 Clothes Colours
> COLOUR
> red
> blue
> green
> yellow
>
> Table2 Clothes
> COLOUR ITEM
> red pullover
> green jumper
> green trousers
> yellow shirt
> red hat
> red scarf
> red coat
>
> I want to be able to create a Select with two fields COLOUR, ITEMs,
> which does the following, where ITEMS is concatenated from each
> matching group record in Table2 by COLOUR
> COLOUR ITEMS
> red pullover: hat: scarf: coat
> green jumper: trousers
> yellow shirt
>
>
> I hope I have explained it correctly.
>
> Any help or pointers, would be most appreciated, even if it is - NOT
> POSSIBLE
>
> Thanks
>
> Richard


maybe you can start with this, and work on it to get what you need:

mysql> select * from PROJECTS;
+--------+----------+
| projid | projname |
+--------+----------+
| 1 | Bravo |
| 2 | Alhpa |
| 3 | Omega |
+--------+----------+
3 rows in set (0.00 sec)

mysql> set @name=""; select (projid mod 2) as k, projid,
@name:=concat(@name,': ',projname) from PROJECTS;
Query OK, 0 rows affected (0.00 sec)

+---+--------+------------------------------------+
| k | projid | @name:=concat(@name,': ',projname) |
+---+--------+------------------------------------+
| 1 | 1 | : Bravo |
| 0 | 2 | : Bravo: Alhpa |
| 1 | 3 | : Bravo: Alhpa: Omega |
+---+--------+------------------------------------+
3 rows in set (0.00 sec)



--
Luuk
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-16-2008, 01:40 PM
Rik Wasmus
 
Posts: n/a
Default Re: Brain Not Working: Summary Fields

On Tue, 13 May 2008 19:34:19 +0200, mcl <mcl.office@googlemail.com> wrote:

> I would like to create an sql select that listed all group records as
> a concatenated string, but I just can not work out how to do it.
>
> eg Table1 Clothes Colours
> COLOUR
> red
> blue
> green
> yellow
>
> Table2 Clothes
> COLOUR ITEM
> red pullover
> green jumper
> green trousers
> yellow shirt
> red hat
> red scarf
> red coat
>
> I want to be able to create a Select with two fields COLOUR, ITEMs,
> which does the following, where ITEMS is concatenated from each
> matching group record in Table2 by COLOUR
> COLOUR ITEMS
> red pullover: hat: scarf: coat
> green jumper: trousers
> yellow shirt
>
>
> I hope I have explained it correctly.
>
> Any help or pointers, would be most appreciated, even if it is - NOT
> POSSIBLE



SELECT x.COLOUR GROUP_CONCAT(y.ITEM SEPARATOR ': ')
FROM ClothesColours x
LEFT JOIN Clothes y
ON y.COLOUR = x.COLOUR
GROUP BY x.COLOUR

Not very portable though, if I recall correctly GROUP_CONCAT() is
something just MySQL does.
--
Rik Wasmus
[SPAM] Now temporarily looking for some smaller PHP/MySQL projects/work to
fund a self developed bigger project, mail me at rik at rwasmus.nl. [/SPAM]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:53 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com