mysql显示各专业总人数_mysql实现每个专业分数段统计人数

本文展示了如何使用MySQL查询统计不同分数段的学生人数,包括按专业分组和整体统计。通过CASE语句结合BETWEEN操作符,可以动态生成SQL查询,覆盖从200到700的分数区间,为教育数据分析提供便利。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ed1b9b9c7676c51121944a9fb2a2350f.png

我的表结构student_info| id |name |profession|score||--|--|--|--||id|姓名|分数|专业|

按分数段统计

400到500人数,300到400人数selectcount(case when score between 400 and 500 then 1 end) as 400到500,count(case when score between 300 and 400 then 1 end) as 300到400from student_info;

3a31469d7cf03ac4db292d40ccbfaddb.png

按分数段和专业统计

400到500人数,300到400人数selectcount(case when score between 400 and 500 then 1 end) as 400到500,count(case when score between 300 and 400 then 1 end) as 300到400from student_info GROUP BY profession;

f4e35ac713517b1e34f582b6f9627c3a.png

sql动态拼接生成int start = 200; int end = 700; int inter = 10; int count = (end-start)/inter; StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("select "); for(int i =1;i<=count;i++){ int next = start+inter-1; System.out.println(start + " \t" + next); sqlBuilder.append(" count(case when admission_score between ").append(start).append(" and ").append(next).append(" then 1 end) as ").append(start).append("到").append(next); if(i!=count){ sqlBuilder.append(", "); } start += inter; } sqlBuilder.append(" from z_student_info"); System.out.println(sqlBuilder.toString());

输出sqlselect count(case when admission_score between 200 and 209 then 1 end) as 200到209, count(case when admission_score between 210 and 219 then 1 end) as 210到219, count(case when admission_score between 220 and 229 then 1 end) as 220到229, count(case when admission_score between 230 and 239 then 1 end) as 230到239, count(case when admission_score between 240 and 249 then 1 end) as 240到249, count(case when admission_score between 250 and 259 then 1 end) as 250到259, count(case when admission_score between 260 and 269 then 1 end) as 260到269, count(case when admission_score between 270 and 279 then 1 end) as 270到279, count(case when admission_score between 280 and 289 then 1 end) as 280到289, count(case when admission_score between 290 and 299 then 1 end) as 290到299, count(case when admission_score between 300 and 309 then 1 end) as 300到309, count(case when admission_score between 310 and 319 then 1 end) as 310到319, count(case when admission_score between 320 and 329 then 1 end) as 320到329, count(case when admission_score between 330 and 339 then 1 end) as 330到339, count(case when admission_score between 340 and 349 then 1 end) as 340到349, count(case when admission_score between 350 and 359 then 1 end) as 350到359, count(case when admission_score between 360 and 369 then 1 end) as 360到369, count(case when admission_score between 370 and 379 then 1 end) as 370到379, count(case when admission_score between 380 and 389 then 1 end) as 380到389, count(case when admission_score between 390 and 399 then 1 end) as 390到399, count(case when admission_score between 400 and 409 then 1 end) as 400到409, count(case when admission_score between 410 and 419 then 1 end) as 410到419, count(case when admission_score between 420 and 429 then 1 end) as 420到429, count(case when admission_score between 430 and 439 then 1 end) as 430到439, count(case when admission_score between 440 and 449 then 1 end) as 440到449, count(case when admission_score between 450 and 459 then 1 end) as 450到459, count(case when admission_score between 460 and 469 then 1 end) as 460到469, count(case when admission_score between 470 and 479 then 1 end) as 470到479, count(case when admission_score between 480 and 489 then 1 end) as 480到489, count(case when admission_score between 490 and 499 then 1 end) as 490到499, count(case when admission_score between 500 and 509 then 1 end) as 500到509, count(case when admission_score between 510 and 519 then 1 end) as 510到519, count(case when admission_score between 520 and 529 then 1 end) as 520到529, count(case when admission_score between 530 and 539 then 1 end) as 530到539, count(case when admission_score between 540 and 549 then 1 end) as 540到549, count(case when admission_score between 550 and 559 then 1 end) as 550到559, count(case when admission_score between 560 and 569 then 1 end) as 560到569, count(case when admission_score between 570 and 579 then 1 end) as 570到579, count(case when admission_score between 580 and 589 then 1 end) as 580到589, count(case when admission_score between 590 and 599 then 1 end) as 590到599, count(case when admission_score between 600 and 609 then 1 end) as 600到609, count(case when admission_score between 610 and 619 then 1 end) as 610到619, count(case when admission_score between 620 and 629 then 1 end) as 620到629, count(case when admission_score between 630 and 639 then 1 end) as 630到639, count(case when admission_score between 640 and 649 then 1 end) as 640到649, count(case when admission_score between 650 and 659 then 1 end) as 650到659, count(case when admission_score between 660 and 669 then 1 end) as 660到669, count(case when admission_score between 670 and 679 then 1 end) as 670到679, count(case when admission_score between 680 and 689 then 1 end) as 680到689, count(case when admission_score between 690 and 699 then 1 end) as 690到699 from z_student_info

推荐mysql视频教程,地址:https://www.php.cn/course/list/51.html

### SQL 实现方案 以下是针对问题的具体解决方案: #### 1) 统计学生总人数 可以通过 `COUNT` 函数来统计学生总数。假设存在一张名为 `students` 的表,其中包含学生的记录。 ```sql SELECT COUNT(*) AS total_students FROM students; ``` 此查询返回学生总人数[^1]。 --- #### 2) 查询姓张的学生基本信息 如果学生信息存储在 `students` 表中,并且有一个字段表示姓名(如 `name`),可以使用 `LIKE` 来匹配以“张”开头的名字。 ```sql SELECT * FROM students WHERE name LIKE '张%'; ``` 上述语句会筛选出所有名字以“张”开头的学生的信息[^2]。 --- #### 3) 获取每位学生的每门课成绩(含姓名、课程名、分数) 假设有两张表:`students` `scores`,分别存储学生信息成绩信息。通过连接这两张表可以获得所需数据。 ```sql SELECT s.name, sc.course_name, sc.score FROM students s JOIN scores sc ON s.student_id = sc.student_id; ``` 这里利用了 `JOIN` 将两个表关联起来,从而获取每位学生的每门课成绩及其对应的姓名课程名称[^3]。 --- #### 4) 增加对应授课老师的姓名 为了进一步扩展第三点的需求,在原有基础上加入教师信息。假设有一张 `teachers` 表用于存储教师信息,同时还有一个中间表 `course_teachers` 存储课程与教师的关系,则可通过多重联结实现目标。 ```sql SELECT s.name AS student_name, c.course_name, sc.score, t.teacher_name FROM students s JOIN scores sc ON s.student_id = sc.student_id JOIN courses c ON sc.course_id = c.course_id JOIN course_teachers ct ON c.course_id = ct.course_id JOIN teachers t ON ct.teacher_id = t.teacher_id; ``` 这段代码不仅提供了学生的成绩详情,还附加了负责该课程的老师姓名[^4]。 --- #### 5) 计算各科平均分并将结果存入`t_score_result`表 要计算每一门课程的平均得分并将其保存到指定的目标表中,可采用以下方法: 先创建目标表结构(如果尚未建立的话): ```sql CREATE TABLE IF NOT EXISTS t_score_result ( course_name VARCHAR(255), avg_score DECIMAL(10, 2) ); ``` 接着执行插入操作的同时完成聚合运算: ```sql INSERT INTO t_score_result (course_name, avg_score) SELECT c.course_name, AVG(sc.score) AS average_score FROM scores sc JOIN courses c ON sc.course_id = c.course_id GROUP BY c.course_name; ``` 这样就实现了按学科分类求取均值的功能,并把最终成果写进了新表里[^5]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值