If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > HOWTO select several rows in one comma- separated

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-21-07, 05:37
almu_alf almu_alf is offline
Registered User
 
Join Date: Aug 2007
Posts: 4
HOWTO select several rows in one comma- separated

Hello!!

First of all, thank you in advance for helping me!!!

My problem is that I have a BBDD with a table like this (in MySql)

E.g.

+---------+-----------+
| groupId | serviceId |
+---------+-----------+
| grup1 | service1 |
| grup1 | service2 |
| grup1 | service3 |
| grup2 | service1 |
| grup2 | service2 |
+---------+-----------+

And I need to do a select o a procedure or something that returns me something like this:

+---------+------------------------------------------+
| groupId | serviceId |
+---------+------------------------------------------+
| grup1 | service1, service2, service3 |
+---------+------------------------------------------+

Is this possible in any way????

Than you very very much.

Almu
Reply With Quote
  #2 (permalink)  
Old 08-21-07, 06:54
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Almu

If you have a function then you could just loop through the records adding each item to a string and then return the string. The problems with this is that it's poor design - what happens when the number of items exceeds the max string length for MySQL (255 chars I believe).

select get_all_service_ids( groupId );

Better to just pull all the items back as a normal select then use PHP (or whatever your front end is written in) to build the string.

What's a BBDD?

Mike
Reply With Quote
  #3 (permalink)  
Old 08-21-07, 07:10
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Is there any max number of services per group?
Unfortunately you're delving into the realms of recursion as Mike suggested, which is horrible!

What is the justification for showing the results in this way?
__________________
George
Twitter | Blog
Reply With Quote
  #4 (permalink)  
Old 08-21-07, 08:08
almu_alf almu_alf is offline
Registered User
 
Join Date: Aug 2007
Posts: 4
Thank you for you answers!!!

Unfortunately there isn't a maximun number of services per group...

I'll try to post process them after the select...

Than you anyway!
Reply With Quote
  #5 (permalink)  
Old 08-21-07, 08:17
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
I haven't got MySQL where I am but the code would be similar to the following but you'd want to check the size of the resulting string:

Code:
create function  get_all_service_ids( 
    p_groupId varchar(20) 
) returns varchar(255)
as
begin
    declare v_loop varchar(20);
    declare v_str varchar(255);

    select  min(serviceId) into v_loop
    from    MyTable
    where  groupId = p_groupId;

    while ( v_loop is not null ) do
       if v_str is null then
           set v_str = v_loop;
       else
           set v_str = concat( v_str, ',',  v_loop );
       end if;

       select  min(serviceId) into v_loop
       from    MyTable
       where  groupId = p_groupId
                 and serviceId > v_loop;
    end while;

    return( v_str );
end;
Quote:
you're delving into the realms of recursion
Strictly speaking it's iteration.

Mike
Reply With Quote
  #6 (permalink)  
Old 08-21-07, 08:28
dbmab dbmab is offline
Registered User
 
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
Reply With Quote
  #7 (permalink)  
Old 08-21-07, 08:41
almu_alf almu_alf is offline
Registered User
 
Join Date: Aug 2007
Posts: 4
Thank youuuuu a lot!!

Someone has given me the solution, it's with the function GROUP_CONCAT that you've told me:

SELECT groupId, GROUP_CONCAT(serviceId) AS serviceIds FROM group_services where groupId=’grupo1’ GROUP BY groupId;

But I think that the problem of the max size is still there, isn't it?

Almu
Reply With Quote
  #8 (permalink)  
Old 08-21-07, 08:54
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Quote:
Originally Posted by mike_bike_kite
Strictly speaking it's iteration.
What's the difference?
__________________
George
Twitter | Blog
Reply With Quote
  #9 (permalink)  
Old 08-21-07, 09:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
if you're only running the query for a single group at a time, then you can simplify this --

SELECT groupId, GROUP_CONCAT(serviceId) AS serviceIds FROM group_services where groupId='grupo1' GROUP BY groupId;

to this --

SELECT GROUP_CONCAT(serviceId) AS serviceIds FROM group_services where groupId='grupo1'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 08-21-07, 09:10
almu_alf almu_alf is offline
Registered User
 
Join Date: Aug 2007
Posts: 4
Oh, yes, that second one is what I need!

Thank you!!!
Reply With Quote
  #11 (permalink)  
Old 08-21-07, 10:23
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Learnt something there - thanks.
Mike
Reply With Quote
  #12 (permalink)  
Old 08-21-07, 10:40
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by georgev
What's the difference?
iteration: is just a loop (for, while etc). The idea is you repeat a process, usually on each item in an array etc, until finished.

recursion: is where a function that calls itself repeatedly until a condition is met. This isn't done in a loop but by going ever deeper into into itself.

Games playing programs such as this checkers program would iterate through all the moves at the current level and after each move recursively call itself to play all the moves of the oponent. This is called a Min-Max search tree where we look for the best move for us (the max score) followed by the best oponents move (the min score for us).

Mike
Reply With Quote
  #13 (permalink)  
Old 08-21-07, 10:42
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Wow, I pretty much hit the nail on the head with the difference between the two (they were in my head and I comtemplated having a guess at them ).

Thanks Mike
__________________
George
Twitter | Blog
Reply With Quote
  #14 (permalink)  
Old 08-21-07, 10:54
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
If you get some time then have a play at the checkers program, it was an earlly attempt by me at a Java applet but it plays reasonably well.
Mike
Reply With Quote
  #15 (permalink)  
Old 08-21-07, 10:55
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
I've actually played it before
__________________
George
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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