views:

27

answers:

2

Hi, I'm trying to get a column result in a single row. Is it possible in My sql say I

Select name from users where city = 'NewYork'

now this will result in

name
mak
sandy
john

Can I get the result in this

name  
mak,sandy,john

I Mysql concat_ws() function does concat but its not showing me result.

A: 

You may want to use the GROUP_CONCAT() function:

SELECT GROUP_CONCAT(name) AS name FROM users WHERE city = 'NewYork';

Test case:

CREATE TABLE users (name varchar(10), city varchar(10));
INSERT INTO users VALUES ('mak', 'NewYork');
INSERT INTO users VALUES ('sandy', 'NewYork');
INSERT INTO users VALUES ('john', 'NewYork');
INSERT INTO users VALUES ('paul', 'Washington');

Result:

+----------------+
| name           |
+----------------+
| mak,sandy,john |
+----------------+
1 row in set (0.00 sec)
Daniel Vassallo
Hey thanks for your answer.
MakDotGNU
A: 

Is

SELECT GROUP_CONCAT(name SEPARATOR ',') FROM users WHERE city = 'NewYork'

suit you ?

S0pra