我希望MySQL查询结果在同一行

时间:2017-03-24 13:21:27

标签: mysql sql database

SELECT sum(case when (gender)=1 THEN 1 ELSE 0 END),
       GROUP_CONCAT(sum(case when (gender)=2 THEN 1 ELSE 0 END) SEPARATOR ' ') as combine         
from family_member_tbl
GROUP BY gender 

1 个答案:

答案 0 :(得分:1)

不,你不能像那样嵌套分组功能。而是首先得到总和然后group_concat()喜欢

select sum_1, sum_2, group_concat(sum_2) as combine
from (
SELECT gender,
       sum(case when gender = 1 THEN 1 ELSE 0 END) as sum_1,
       sum(case when gender = 2 THEN 1 ELSE 0 END) as sum_2         
from family_member_tbl
GROUP BY gender ) xxx
group by gender;