Skip to content
Advertisement

Using COUNT in GROUP_CONCAT

This is my table:

The result I want should be string “3, 2, 3, 1” (count of items that belong to each company), because this is just part of my complex query string.

I tried to use this query:

But I got an error:

Error Number: 1111
Invalid use of group function

I have a feeling COUNT, MAX, MIN or SUM can’t be used in GROUP_CONCAT. If so, do you know another way to do this?

Advertisement

Answer

You need to COUNT() with GROUP BY in an inner SELECT clause first and then apply GROUP_CONCAT();

Output:

|   CNT   |
-----------
| 3,2,3,1 |

Here is SQLFiddle demo.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement