this is my SQL question
Table – Activity
id activity userID
0 99 1
1 99 2
2 99 3
3 88 1
4 77 2
5 77 3
I hope to query this table to generate such a result which pools the choice of activity by
different users:
99 1 2 3
88 1
77 2 3
My first attempt is using JOIN to self-join the table recursively. But it is a headache. Anyone has other suggestion?
Thanks
Edit:
Another problem. How about if I want to check whether users are friends before grouping?
Table – Friends
id userID friendID
0 1 2
1 2 1
2 1 3
3 3 1
4 2 3
5 3 2
So they are friends (in both directions). Then I want to do the pooling (GROUP BY) given that they are friends again.
Again, thanks. You guys offer me great guidance.
Use
GROUP BYactivity and theGROUP_CONCAT()Like this code:
SEE THE FIDDLE