2017-01-25 60 views

回答

2

這做什麼你描述:

create table NoOneEverNamesTheTableInSqlQuestions (
    id int, 
    name text, 
    groupid int 
); 

insert into NoOneEverNamesTheTableInSqlQuestions values 
    (1, 'user1', 0), 
    (2, 'user2', 0), 
    (3, 'user3', 1), 
    (4, 'user4', 1); 

select group_concat(g separator '---') as output 
from (
    select group_concat(concat_ws(',',id,name) separator ';') as g 
    from NoOneEverNamesTheTableInSqlQuestions 
    group by groupid 
) as g; 

輸出,與MySQL 8.0.0-DMR測試:

+-----------------------------------+ 
| output       | 
+-----------------------------------+ 
| 1,user1;2,user2---3,user3;4,user4 | 
+-----------------------------------+ 

但我不知道你爲什麼想要這樣做。這似乎是在應用程序代碼中更容易做的事情。

+0

good ans。我學習新的分隔符 –