mysql 统计语句 年龄段

cfd406635982 2010-10-09 03:34:03
在网上看到的。。

data.txt内数据如下:

1,tonny,18
1,wendy,30
1,james,25
1,simon,22
1,flynn,35
1,nancy,42
1,olivia,51
1,linda,20


create table u(
id int,
name varchar(8),
age int
);


Load Data InFile 'C:/data.txt' Into Table `u` Fields Terminated By ',';


mysql> select * from u;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 1 | tonny | 18 |
| 1 | wendy | 30 |
| 1 | james | 25 |
| 1 | simon | 22 |
| 1 | flynn | 35 |
| 1 | nancy | 42 |
| 1 | olivia | 51 |
| 1 | linda | 20 |
+------+--------+------+
8 rows in set (0.00 sec)


要求得到效果如下:
统计出不同年龄段的人数:

年龄段 人数
18-20 2
21-30 3
31-40 1
41-50 1
> 50 1

怎么处理? 谢谢 。。
...全文
488 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwwb 2010-10-09
  • 打赏
  • 举报
回复
SELECT COUNT(*),ELT(CEILING(age/10)-1,'18-20','21-30','31-40','41-50','> 50') FROM u GROUP BY
ELT(CEILING(age/10)-1,'18-20','21-30','31-40','41-50','> 50')
cfd406635982 2010-10-09
  • 打赏
  • 举报
回复
越来越喜欢这个论坛了 呵呵
有问题就会有人解决。
而且能学到很多东西。。
我现在没事就在学习已经解决了的东西 呵呵
cfd406635982 2010-10-09
  • 打赏
  • 举报
回复
越来越喜欢这个论坛了 呵呵
有问题就会有人解决。
而且能学到很多东西。。
我现在没事就在学习已经解决了的东西 呵呵
cfd406635982 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 cfd406635982 的回复:]
在网上看到的。。

data.txt内数据如下:

1,tonny,18
1,wendy,30
1,james,25
1,simon,22
1,flynn,35
1,nancy,42
1,olivia,51
1,linda,20


SQL code

create table u(
id int,
name varchar(8),
age int
);
……
[/Quote]
非常佩服。
rucypli 2010-10-09
  • 打赏
  • 举报
回复
select age,count(*) from (
select age,case when age between 10 and 20 then 1
when age between 21 and 30 then 2
when age between 31 and 40 then 3
end age1
from u
)T
group by age,age1
zuoxingyu 2010-10-09
  • 打赏
  • 举报
回复
在CSDN里混,随时有惊喜。
zuoxingyu 2010-10-09
  • 打赏
  • 举报
回复
版主彪悍,V5。

收藏了。
ACMAIN_CHM 2010-10-09
  • 打赏
  • 举报
回复
mysql> select * from u;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 1 | tonny | 18 |
| 1 | wendy | 30 |
| 1 | james | 25 |
| 1 | simon | 22 |
| 1 | flynn | 35 |
| 1 | nancy | 42 |
| 1 | olivia | 51 |
| 1 | linda | 20 |
+------+--------+------+
8 rows in set (0.00 sec)

mysql> select elt(interval(age,18,21,31,41,51),
-> '18-20',
-> '21-30',
-> '31-40',
-> '41-50',
-> '> 50'
-> ) as `年龄段`
-> ,count(*) as `人数`
-> from u
-> group by elt(interval(age,18,21,31,41,51),
-> '18-20',
-> '21-30',
-> '31-40',
-> '41-50',
-> '> 50'
-> );
+--------+------+
| 年龄段 | 人数 |
+--------+------+
| 18-20 | 2 |
| 21-30 | 3 |
| 31-40 | 1 |
| 41-50 | 1 |
| > 50 | 1 |
+--------+------+
5 rows in set (0.03 sec)

mysql>

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧