MoerPerfect 2022-07-25 23:53 采纳率: 100%
浏览 64
已结题

MySQL查询上的问题

遇到一个关于MySQL查询上的问题,题目如下:我写的建表语句在代码文段中,方便解决,你能够直接入手解决问题,减掉那些不必要的麻烦,直接入手帮忙。
问题:最后第4题做了很久都没达到题目要求,其中我的不完整查询语句也在其中,希望各位能帮忙解决一下,解决成功的必定采纳

–1.学生表
students_test(stu_id,stu_name,stu_birth,stu_sex) –学生编号(主键,自增),学生姓名, 出生年月,性别
–2.课程表
subjects_test(subj_id,subj_name,tea_id) –课程编号(主键,自增), 课程名称, 教师编号(外键)
–3.教师表
teachers_test(tea_id,tea_name) –教师编号(主键,自增),教师姓名
–4.成绩表
scores_test(stu_id,subj_id,sco_result) –学生编号,课程编号,分数
插入数据
--插入学生表测试数据
'1','刘一' , '2000-01-01' , '男';
'2','陈二' , '1999-10-01' , '女';
'3','张三' , '2001-11-11' , '男';
'4','李四' , '2002-09-08' , '女';
'5','王五' , '1998-12-01' , '男';
'6','赵六' , '1996-10-01' , '女';
'7','孙七' , '1995-05-01' , '男';
'8','周八' , '2003-08-01' , '女';
'9','吴九' , '2002-09-10' , '男';
'10','郑十' , '2001-02-02' , '女';
--课程表测试数据
'1','JAVA' , '3';
'2','MYSQL' , '1';
'3','HTML' , '2';
--教师表测试数据
'1' , '朱老';
'2' , '万老';
'3' , '王老';
--成绩表测试数据
'1', '1', 70;
'1', '2', 80;
'1', '3', 89;
'2', '1', 90;
'2', '2', 50;
'3', '1', 80;
'3', '2', 70;
'4', '1', 90;
'4', '2', 30;
'4', '3', 40;
'5', '1', 76;
'5', '2', 87;
'5', '3', 88;
'6', '1', 41;
'6', '3', 54;
'7', '2', 79;
'7', '3', 68;
'8', '1', 91;
'8', '3', 74;
'9', '2', 81;
'9', '3', 92;
'10', '2', 55;
编写sql查询语句:
1)查询朱老所教课程,且分数低于70的学生姓名和分数
2)查询平均成绩不及格的同学的学生编号和学生姓名和平均成绩
3)查询学生信息的学生编号、学生姓名、选课总数、所有课程的总成绩
4)查询所有学生的所有课程的成绩以及平均成绩,按照平均成绩降序排序
要求:ID JAVA MYSQL HTML AVG_SCORE

# -1.学生表 
# students_test(stu_id,stu_name,stu_birth,stu_sex) –学生编号(主键,自增),学生姓名, 出生年月,性别 
drop table if exists students_test;
create table if not exists students_test(
    stu_id int primary key auto_increment comment '学生编号',
    stu_name varchar(50) not null comment '学生姓名',
    stu_birth date comment '出生年月',
    stu_sex varchar(1) not null comment '性别'
)engine = innodb default charset = utf8 comment '学生表';

# 2.课程表 
# subjects_test(subj_id,subj_name,tea_id) –课程编号(主键,自增), 课程名称, 教师编号(外键) 
drop table if exists subjects_test;
create table if not exists subjects_test(
    subj_id int primary key auto_increment comment '课程编号',
    subj_name varchar(50) not null comment '课程名称',
    tea_id int comment '教师编号',
    constraint fk_tea_id foreign key (tea_id) references teachers_test(tea_id)
)engine = innodb default charset = utf8 comment '课程表';

# 3.教师表 
# teachers_test(tea_id,tea_name) –教师编号(主键,自增),教师姓名 
drop table if exists teachers_test;
create table if not exists teachers_test(
    tea_id int primary key auto_increment comment '教师编号',
    tea_name varchar(50) comment '教师姓名'
)engine = innodb default charset = utf8 comment '教师表';

# 4.成绩表 
# scores_test(stu_id,subj_id,sco_result) –学生编号,课程编号,分数
drop table if exists scores_test;
create table if not exists scores_test(
    stu_id int not null comment '学生编号',
    subj_id int not null comment '课程编号',
    sco_result int not null comment '分数'
)engine = innodb default charset = utf8 comment '成绩表';

# 插入数据
# 插入学生表测试数据
insert into students_test values ('1','刘一' , '2000-01-01' , '男'),('2','陈二' , '1999-10-01' , '女'),('3','张三' , '2001-11-11' , '男'),('4','李四' , '2002-09-08' , '女'),('5','王五' , '1998-12-01' , '男'),('6','赵六' , '1996-10-01' , '女'),('7','孙七' , '1995-05-01' , '男'),('8','周八' , '2003-08-01' , '女'),('9','吴九' , '2002-09-10' , '男'),('10','郑十' , '2001-02-02' , '女');

# 查看学生表数据
select * from students_test;

# 插入课程表测试数据
insert into subjects_test values ('1','JAVA','3'),('2','MYSQL','1'),('3','HTML','2');
# 查看课程表
select * from subjects_test;

# 插入教师表数据
insert into teachers_test values ('1','朱老师'),('2','万老师'),('3','王老师');
# 查看教师表
select * from teachers_test;

# 插入成绩表数据
insert into scores_test values ('1','1',70),('1','2',80),('1','3',89),('2','1',90),('2','2',50),('3','1',80),('3','2',70),('4','1',90),('4','2',30),('4','3',40),('5','1',76),('5','2',87),('5','3',88),('6','1',41),('6','3',54),('7','2',79),('7','3',68),('8','1',91),('8','3',74),('9','2',81),('9','3',92),('10','2',55);
# 查看成绩表
select * from scores_test;

# 编写sql查询语句:
# 4)查询所有学生的所有课程的成绩以及平均成绩,按照平均成绩降序排序
# 要求:ID JAVA MYSQL HTML AVG_SCORE
# 这一段的代码的问题是有的学生没部分科目成绩,但是被覆盖上上一位的成绩了
select sc1.stu_id 'ID',
ja.cj 'JAVA',
mysql.cj 'MYSQL',
html.cj 'HTML'
from scores_test sc1,subjects_test su1,
(select stu_id id,sco_result cj from scores_test sc2,subjects_test su2 where su2.subj_id = 1 and sc2.subj_id = su2.subj_id) ja,
(select stu_id id,sco_result cj from scores_test sc2,subjects_test su2 where su2.subj_id = 2 and sc2.subj_id = su2.subj_id) mysql,
(select stu_id id,sco_result cj from scores_test sc2,subjects_test su2 where su2.subj_id = 3 and sc2.subj_id = su2.subj_id) html
where sc1.subj_id = su1.subj_id or ja.id = sc1.stu_id or mysql.id = sc1.stu_id or html.id = sc1.stu_id group by sc1.stu_id;

# 这一段的问题是,加上平均成绩后,上一段代码的其他内容都被一名学生的成绩覆盖了,只有平均成绩是对的
select sc1.stu_id 'ID',
ja.cj 'JAVA',
mysql.cj 'MYSQL',
html.cj 'HTML',
pj.pjcj 'AVG_SCORE'
from scores_test sc1,subjects_test su1,
(select stu_id id,sco_result cj from scores_test sc2,subjects_test su2 where su2.subj_id = 1 and sc2.subj_id = su2.subj_id) ja,
(select stu_id id,sco_result cj from scores_test sc2,subjects_test su2 where su2.subj_id = 2 and sc2.subj_id = su2.subj_id) mysql,
(select stu_id id,sco_result cj from scores_test sc2,subjects_test su2 where su2.subj_id = 3 and sc2.subj_id = su2.subj_id) html,
(select stu_id id,avg(sco_result) pjcj from scores_test sc2 group by sc2.stu_id) pj
where sc1.subj_id = su1.subj_id or ja.id = sc1.stu_id or mysql.id = sc1.stu_id or html.id = sc1.stu_id or pj.id = sc1.stu_id or ja.id = mysql.id or mysql.id = html.id or html.id = pj.id group by pj.pjcj desc;

  • 写回答

2条回答 默认 最新

  • 关注
    select a.SID ID , max(a.JAVA) JAVA ,max(b.MYSQL) MYSQL,max(c.HTML) HTML ,(d.total/3) avg_score
    FROM
    (select  sc1.stu_id SID,(case st1.subj_name when "JAVA" then sc1.sco_result end) JAVA from subjects_test st1,scores_test sc1 where st1.subj_id = sc1.subj_id)a
        left join
        (select  sc2.stu_id SID,(case st2.subj_name when "MYSQL" then sc2.sco_result end) MYSQL from subjects_test st2,scores_test sc2 where st2.subj_id = sc2.subj_id) b
    on a.sid =  b.sid
        left join
         (select  sc3.stu_id SID,(case st3.subj_name when "HTML" then sc3.sco_result end) HTML from subjects_test st3,scores_test sc3 where st3.subj_id = sc3.subj_id) c
    on a.SID = c.SID
        #计算平均分
    left join(select sc4.stu_id SID, sum(sc4.sco_result) total from scores_test sc4  group by sc4.stu_id) d
    
    on a.SID = d.SID
    
    group by id;
    

    如果报错:sql_mode=only_full_group_by;https://blog.csdn.net/m0_37550986/article/details/116796509

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月3日
  • 已采纳回答 7月26日
  • 创建了问题 7月25日

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题