当前位置:数据库 > MySQL >>

mysql查询问题!

补充:人员情况表(employee),里面有一字段文化程度(wh),包括四种情况(本科以上 大专 高中 初中以下),现在根据年龄字段(age)查询统计出:表中文化程度为本科以上,大专,高中,初中以下,各有多少人,占总人数的多少
答案:建个表:
desc student;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| wh    | int(11) | YES  |     | NULL    |       |
| age   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
wh用数字表示
1:本科,2:大专,3:高中。4:初中以下
---------------------------------------------------------------
普通查询:
select * from student;
+------+------+
| wh   | age  |
+------+------+
|    1 |   22 |
|    1 |   23 |
|    1 |   25 |
|    2 |   25 |
|    2 |   26 |
|    2 |   22 |
|    2 |   27 |
|    2 |   23 |
|    3 |   23 |
|    3 |   23 |
|    3 |   25 |
|    3 |   20 |
|    4 |   20 |
|    4 |   20 |
|    4 |   29 |
+------+------+
15 rows in set (0.00 sec)
--------------------------------------
按你要求查询:
select age, count((case when wh=1 then wh end)) as '1' ,count((case when wh=2 then wh end)) as '2',count((case when wh=3 then wh end)) as '3',count((case when wh=4 then wh end)) as '4' from student group by age;
+------+---+---+---+---+
| age  | 1 | 2 | 3 | 4 |
+------+---+---+---+---+
|   20 | 0 | 0 | 1 | 2 |
|   22 | 1 | 1 | 0 | 0 |
|   23 | 1 | 1 | 2 | 0 |
|   25 | 1 | 1 | 1 | 0 |
|   26 | 0 | 1 | 0 | 0 |
|   27 | 0 | 1 | 0 | 0 |
|   29 | 0 | 0 | 0 | 1 |
+------+---+---+---+---+
7 rows in set (0.00 sec)

上一个:请问JSP服务器端怎么获取客户端提交的图片?获取之后又该怎么保存到mysql数据库呢?
下一个:高分,急啊,mysql上怎么用vc++编嵌入式sql(就是EXEC SQL这种) 刚入门,小白一个,求详解,好的话有追加

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,