List:General Discussion« Previous MessageNext Message »
From:Jasper Bryant-Greene Date:September 16 2005 9:50am
Subject:Re: Creating a string from a resultset directly in SQL ?
View as plain text  
Henri-Maxime Ducoulombier wrote:
> I have a question regarding the possibility of creating a string from a
> resultset directly using an SQL command. I'm not sure if this is possible.
> 
> Here is what I have:
> A table with links between articles and category in a catalog.
> For ArticleID #1, I have say 3 categories, and I would like the list of
> categories like that : 57,42,36
> 
> So I was trying to do something like that :
> SELECT Concat_WS(',', (SELECT CatID FROM tblcatarticles WHERE ArticleID = 1))
> 
> But it obviously won't work since the subquery returns more than 1 row. My
> question is : is there anyway to do this in one request or do I have to
> browse my recordset and build my list using my favorite programming tool ?

Take a look at the GROUP_CONCAT function, under "functions for use with 
GROUP BY clauses" in the MySQL manual. You'll need MySQL 4.1+ AFAIK.

-- 
Jasper Bryant-Greene
Freelance web developer
http://jasper.bryant-greene.name/
Thread
Creating a string from a resultset directly in SQL ?Henri-Maxime Ducoulombier16 Sep
  • Re: Creating a string from a resultset directly in SQL ?Jasper Bryant-Greene16 Sep
Re: Creating a string from a resultset directly in SQL ?Henri-Maxime Ducoulombier16 Sep