我在 MySQL 中使用 GROUP_CONCAT 时遇到问题 我的表格
g0
如下:
ID Age Sex
-------------
1 16 Male
2 18 Female
3 16 Male
4 18 Female
5 16 Male
但是我需要桌子看起来像这样
ID count
1,3,5 3
2,4 2
我尝试了这个查询:
SELECT GROUP_CONCAT(
CONCAT(cnt)) cnts FROM
(SELECT COUNT(ID) as cnt FROM g0 GROUP BY Age , Sex order by ID Desc) ;
但是我收到此错误消息:
1248. Every derived table must have it's own alias
无需在
count
内包含 group_concat
- 只需将其选择为具有相同 group by
表达式的不同项目即可:
SELECT GROUP_CONCAT(id), COUNT(*)
FROM g0
GROUP BY age, sex
ORDER BY 1 DESC
选择 GROUP_CONCAT( CONCAT(cnt))cnts FROM (SELECT COUNT(ID) as cnt FROM g0 GROUP BY Age , Sex order by ID Desc) AS cnts;