Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CONCAT strings from multiple rows in one SELECT

Status
Not open for further replies.

Broccoli2

MIS
Mar 26, 2001
161
GB
I'd like to be able to return a single cell containing a string formed from all the records in a table, ie
Code:
mysql> SELECT OrganisationName FROM t1;
+------------------+
| OrganisationName |
+------------------+
| this             |
| that             |
| the other        |
+------------------+
I want this:
Code:
+-----------------------+
| SomeSelectFunction    |
+-----------------------+
| this, that, the other |
+-----------------------+
At the moment the best I can do is use a variable, but this takes the same number of rows to build up:
Code:
mysql> SELECT @um:=concat_ws(", ", @um,t1.OrganisationName) FROM t1;
+-----------------------------------------------+
| @um:=concat_ws(", ", @um,t1.OrganisationName) |
+-----------------------------------------------+
| this                                          |
| this, that                                    |
| this, that, the other                         |
+-----------------------------------------------+
Is there a way to return that final value in one SELECT? I tried adding a LENGTH(@um) and ORDER BY but it didn't work. I'd rather not use two SELECTS since I'd like to build this as an Access SQL Pass Through (although I know a workaround to execute multiple SELECTS in one query, I'd rather not use it as it's a bit messy)

Please help!

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top