How to get one column result in one row
I'm trying to get a column result in a sin开发者_如何学JAVAgle row. Is it possible in MySql 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.
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)
Is
SELECT GROUP_CONCAT(name SEPARATOR ',') FROM users WHERE city = 'NewYork'
suit you ?
精彩评论