商务服务
mysql的一些常用操作(二)
2025-01-02 09:23

紧跟上一节,我们创建了四个表:

Student、Teacher、Course、Score



 2.查询课程编号为 01 且课程成绩在 80 分及以上的学生的学号和姓名






4.检索至少选修两门课程的学生学号



5.选修了全部课程的学生信息

select a.* from student a,score b where a.sid=b.sid group by a.sid having count(cid)=(select count(*) from course);

 6 .查询存在不及格的课程

select  distinct a.* from course a,score b where a.cid=b.cid and b.scoreore<60;

 7.查询任何一门课程成绩在 70 分以上的学生姓名、课程名称和分数

select a.sname,b.cname,c.scoreore from student a,course b,score c where a.sid=c.sid and b.cid=c.cid and c.scoreore>70;

8.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况

select a.sname,b.cname, c.scoreore from student a left join score c on a.sid=c.sid left join course b on b.cid=c.cid;

 9.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数

select a.sname,c.scoreore from student a,course b,score c where a.sid=c.sid and b.cid=c.cid and b.cname="数学" and c.scoreore<60;

 10.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩

select a.sid,a.sname,avg(b.scoreore) from student a,score b where a.sid=b.sid group by a.sid having avg(b.scoreore)>=85;

 11.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select cid,avg(scoreore) from score group by cid order by avg(scoreore) desc,cid asc;

12.查询各科成绩最高分、最低分和平均分

以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列



 13.查询男生、女生人数

select ssex,count(*) from student group by ssex;

 14.检索" 01 "课程分数小于 60,按分数降序排列的学生信息

select a.*,b.scoreore from student a,score b where a.sid=b.sid and b.cid="01" and b.scoreore<60 order by scoreore desc;

 15.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

 16.查询没学过"张三"老师讲授的任一门课程的学生姓名

select distinct student.sname from student where student.sid not in

(select c.sid from teacher a,course b,score c where a.tid=b.tid and b.cid=c.cid and a.tname="张三") ;

17.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

select  d.*,c.scoreore from teacher a,course b,score c,student d

where a.tid=b.tid and b.cid=c.cid and d.sid=c.sid and tname="张三" 

order by c.scoreore desc limit 1;

 18.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

目前我们表中没有重复的成绩,先修改一下:update score set score=90 where skey=17;

若是取出张三老师下的所有学生信息和成绩,即:

select  d.*,c.scoreore from teacher a,course b,score c,student d

where a.tid=b.tid and b.cid=c.cid and d.sid=c.sid and tname="张三" 

order by c.scoreore desc;则有:

select  d.*,c.scoreore from teacher a,course b,score c,student d

where a.tid=b.tid and b.cid=c.cid and d.sid=c.sid and tname="张三" and scoreore =

(select max(scoreore) from teacher a,course b,score c where a.tid=b.tid and b.cid=c.cid and a.tname="张三");

19.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

select any_value(a.sid),any_value(a.cid),any_value(a.scoreore) from score a

inner join score b on a.sid=b.sid and a.cid!=b.cid and a.scoreore=b.scoreore group by a.cid,b.cid;

20.查询每门功成绩最好的前两名


 21.查询每门课程被选修的学生数

select cid,count(*) from score group by cid;

 22.查询出只选修两门课程的学生学号和姓名

select a.sid,a.sname from student a,score b where a.sid=b.sid group by a.sid having count(*)=2;

 23.查询同名学生名单,并统计同名人数

select sname,count(*) from student group by sname having count(*)>1;

 24.查询 1990 年出生的学生名单

select sname from student where year(sage)=1990;

 25.查询各学生的年龄.

select sid,sname,timestampdiff(year,sage,curdate()) from student;

 26.查询本周过生日的学生

select sname from student where week(curdate())=week(sage);

没有学生;

27.查询本月过生日的学生

select sname from student where month(curdate())=month(sage);

没有学生;

28.查询「李」姓老师的数量

select count(*) from teacher where tname like "李%";

 29.查有成绩的学生信息

select * from student where sid in (select sid from score);

 30.查询所有同学的学生编号、学生姓名、选课总数、所有课程的成绩总和

 31.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

select a.sid,a.sname,avg(b.scoreore) from student a,score b where a.sid=b.sid having avg(b.scoreore)>60;

 32.查询不存在" 01 "课程但存在" 02 "课程的情况

 33.查询存在" 01 "课程但可能不存在" 02 "课程的情况

select * from score where cid="01";

 34.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺


 35.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数



 36.查询没有学全所有课程的同学的信息



37.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息

    以上就是本篇文章【mysql的一些常用操作(二)】的全部内容了,欢迎阅览 ! 文章地址:https://sicmodule.kub2b.com/news/14587.html
     栏目首页      相关文章      动态      同类文章      热门文章      网站地图      返回首页 企库往资讯移动站 https://sicmodule.kub2b.com/mobile/ , 查看更多   
最新文章
过年无忧 | 一键get这些春节话术!
一键Get这些春节话术~过年无忧新年快乐春节将至,年味渐浓在这温馨又热闹的节日氛围里我们既能品尝各式各样的美味佳肴沉浸于味
2025在新加坡生活的我们将迎来“至暗时刻”:房租飙涨、每个月入不敷出…
聚焦新加坡真是开年暴击!2025年刚开始,还没过新年呢,万事通就出了一身冷汗:今年又是一个物价涨涨涨的年份。在网上一搜“新加
太抽象!太抽象!2024年游戏行业简直太抽象!
年末,DataEye研究院今天整点活,轻松一波。——用数据、新闻盘点2024年国内游戏业有多抽象。回首2024年有产品研发8年烧了数亿,
TikTok会如何收场
TikTok的命运再次悬而不决。在美国下架12小时又恢复运营之后,1月20日,美国总统特朗普签署行政命令,要求TikTok「不卖就禁」法
今天上午10:00,成绩发布!
早安,东台!‍今天是2025年1月22日‍星期三(农历腊月廿三)大美东台,活力满满进取创新、奋斗拼搏最近有哪些新动态?和小东一
农村土地托管服务的理论基础
中国产品流通经纪人协会供销合作行业标准《农产品食品供应商信用评价规范》参编单位征集函中国农产品流通经纪人协会供销合作行业
头上三尺有神明,每个人头顶都有一颗星,当星光消失人也就消失!
每当夜晚降临后,我们抬头看天空,会看到满天的星星,自古以来,人们从没有停止过对星象的观测和研究。古人观测星象,一则是为了
运营师抖音代运营
运营师抖音代运营:掌握流行短视频潮流的神奇职业短视频平台已经成为人们娱乐、学习和社交的重要方式。在众多的短视频平台中,抖
微短剧,2024年“最大赢家”? | 年终盘点
2024,短剧行业大变样。作者 | 张语格编辑 | 趣解商业文娱组“互联网大厂争相入局。”“98%的短剧制作方都在亏钱。”“用户被免
同类第一!20%弹性的人工智能 ETF 科创(588760)今日上市,一键布局科创板优质AI龙头
  最新公告内容显示,广发上证科创板交易型开放式指数投资基金(基金代码:588760;扩位简称: ETF 科创)已于 2025 年 1 月 1