Please don't make it flag (it's not duplicate) i search it on google can't find any answer.
请不要使它标记(它不是重复的)我在谷歌上搜索它找不到任何答案。
I have below result in mysql
下面是mysql。
|18-30 | 30 - 45 | 45 - 60 | 60+
| 28 | 26 | 12 | 5
I want it like following result-
我想要的结果是-
|18-30 | 28 |
|30 -45| 26 |
|45-60 | 12 |
| 60+ | 5 |
How i can get it in mysql?
我怎么用mysql呢?
1 个解决方案
#1
5
The simplest way is union all
:
最简单的办法就是团结一致:
select '18-30' as range, `18-30` as val from t union all
select '30 - 45', `18-30` from t union all
select '45 - 60', `45 - 60` from t union all
select '60+', `60+`;
However, your results are probably coming from a conditional aggregation. I would expect that you can do something like this instead:
然而,您的结果可能来自一个条件聚合。我希望你能做这样的事情:
select (case when age < 30 then '18-29'
when age < 45 then '30-44'
when age < 60 then '45-59'
else '60+'
end) as agegrp,
count(*)
from . . .
where age >= 18
group by agegrp;
order by min(age)
#1
5
The simplest way is union all
:
最简单的办法就是团结一致:
select '18-30' as range, `18-30` as val from t union all
select '30 - 45', `18-30` from t union all
select '45 - 60', `45 - 60` from t union all
select '60+', `60+`;
However, your results are probably coming from a conditional aggregation. I would expect that you can do something like this instead:
然而,您的结果可能来自一个条件聚合。我希望你能做这样的事情:
select (case when age < 30 then '18-29'
when age < 45 then '30-44'
when age < 60 then '45-59'
else '60+'
end) as agegrp,
count(*)
from . . .
where age >= 18
group by agegrp;
order by min(age)