I am having a trouble with using GROUP_CONCAT in MySQL
My tables g0
as follows:
ID Age Sex
-------------
1 16 Male
2 18 Female
3 16 Male
4 18 Female
5 16 Male
But I need the table to look like
ID count
1,3,5 3
2,4 2
I tried this query:
SELECT GROUP_CONCAT(
CONCAT(cnt)) cnts FROM
(SELECT COUNT(ID) as cnt FROM g0 GROUP BY Age , Sex order by ID Desc) ;
But I get this error message:
1248. Every derived table must have it's own alias
There's no need to have the count
inside group_concat
- just select it as a different item with the same group by
expression:
SELECT GROUP_CONCAT(id), COUNT(*)
FROM g0
GROUP BY age, sex
ORDER BY 1 DESC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With