Alex _TNT
Alex _TNT

Reputation: 319

Column as list with comma between

How can I query a database that has the following columns : id, name. With the result having column 'name' rows displayed as list?

Id Name
1 name1
2 name2
3 name3
4 name4

Result: name1,name2,name2,name4

Currently my query looks like this

SELECT name FROM banned

Upvotes: 1

Views: 58

Answers (3)

benjiman
benjiman

Reputation: 35

SELECT GROUP_CONCAT( DISTINCT Name ORDER BY Name SEPARATOR ',' ) FROM banned;

Upvotes: 1

Mureinik
Mureinik

Reputation: 312219

The group_concat aggregate function should do the trick:

SELECT GROUP_CONCAT(name ORDER BY name) AS name
FROM   banned

EDIT:
To answer the question in the comment, you could add a separator clause to replace the comma in the result:

SELECT GROUP_CONCAT(name ORDER BY name SEPARATOR '...') AS name
FROM   banned

Upvotes: 2

Related Questions