– Student sid sname sage ssex
– Teacher tid tname
– Course cid cname tid
– Score sid cid score
– 50、查询下个月过生日的学生
select * from student
where
case when month(‘2019-04-09’)=12 then month(sage)=1 else month(sage)=month(‘2019-04-09’)+1 end
– 注意月份的循环 12月份后面要到1月份,不是13月
– 当前时间date(now()) 精确到每分每秒
– 49、查询本月过生日的学生
select * from student
where month(sage)=month(now())
– 48、查询下周过生日的学生
select * from student
where week(sage,1)=week(date(now()),1)+1
– 47、查询本周过生日的学生
select * from student
where week(sage,1)=week(date(now()),1)
– week的第二个参数代表周一为一周的第一天
– 46、查询各学生的年龄(精确到月份)
– 备注:年份转换成月份,比如结果是1.9,ditediff 最后取1年
select sid,sage,datediff(month,sage,‘2021-5-18’)/12 from student
– 45、 查询选修了全部课程的学生信息(重点)
select sid,count(cid) from score
group by sid
having count(cid)=(select count(*) from course)
– 44、检索至少选修两门课程的学生学号(不重要)
select sid,count(score) from score
group by sid
having count(score) >=2
– 43、统计每门课程的学生选修人数(超过5人的课程才统计)。
– 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列(不重要)
select cid,count(score)
from score
group by cid
having count(score)>5
order by count(score) desc,cid
– 42、查询每门功成绩最好的前两名(同22和25题)
– row_number() over(partition by 按什么分组 order by 按什么排序 ASC/DESC)
select *
from (select st.sid,st.sname,st.sage,st.ssex,cid,score,row_number() over(partition by cid order by score desc) m
from score sc inner join student st on sc.sid=st.sid
) a
where m in (1,2)
– 为什么要写嵌套,为什么不写就报错1054?如下
select sid,cid,score,row_number() over(partition by cid order by score desc) m
from score
where m in (1,2)
– 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 (重点)
select distinct a.sid,a.cid,a.score
from Score a inner join Score b
on a.sid=b.sid
where a.score=b.score and a.cid!=b.cid