Search code examples
mysqlsqlselectgroup-bygroup-concat

how to get count in groupby mysql?


hy..I have table like this
1.tb_result

id | test_id    | result
====================
1  | 1 | A
2  | 1 | A
2  | 2 | A
3  | 3 | B

My quetion is: how I can Count result per test_id
for example to be like this :

result query

id | test_id    | total_result
====================
1  | 1| A(2)
2  | 2| A(1)
3  | 3| B(1)

Thanks


Solution

  • Try this:

    SELECT A.id, 
           A.test_id, 
           GROUP_CONCAT(A.result, '(', COUNT(A.id), ')') AS total_result
    FROM tb_result A 
    GROUP BY A.test_id