Complex group-by MySQL statement
I have a table with the following columns:
boxid - nouns - username
1 - w1,w2,w3 - user1
1 - w3,w2,w4 - user2
1 - w1,w8,w5 - user1
2 - w7,w2,w5 - user2
and at the present I have a query that allows me to extract the following:
SELECT boxid, group_concat(concat(username,":",nouns) SEPARATOR "|") as listn
FROM table group by boxid;
that query gives me back the following:
box1 - user1:w1,w2,w3|user2:w3,w2,w4|user1:w1,w8,w5
However, I would like to get all nouns of the same user without repeating users (but repeating words of every user), something like the following result:
box1 - user1:w1,w2,w3,w1,w8,w5|user2:w3,w2,w4
Can someone show me if this can be done with mysql and how? I have no开发者_JAVA技巧 idea of this...
(I can do this with php, but I think getting the direct result in sql would be faster...)
Thanks in advance...
Try this:
SELECT boxid, group_concat(concat(username,":",nouns_list) SEPARATOR "|") as listn
FROM
(
SELECT boxid, username, group_concat(nouns) as nouns_list
FROM table
GROUP BY boxid, username
) a
GROUP BY boxid
精彩评论