Zsolt Janes Zsolt Janes - 1 month ago 6
MySQL Question

PHP or SQL count colum elements and separate

I dont find the sollution, I have a json file (or i have sql table):

[{"userid ": 1,"rank": 1}, {"userid ": 1,"rank": 2}]


with this colums:

userid || rank
1 || 1
1 || 2
1 || 5
2 || 1
2 || 2
2 || 4
3 || 1


I would collect the user ranks, something like this:

1 -> 1,2,5
2 -> 1,2,4
3 -> 1

Answer

If this is a SQL database table you could just group the rows and concat the rank column:

SELECT userid, GROUP_CONCAT(rank)
FROM table
GROUP BY userid