数据分析 | MySQL45道练习题(37~45)

陬者

关注

阅读 77

2022-02-03

37.统计每门课程的学生选修人数(超过5人的课程才统计)
-- 【分析】分组统计 

select cid,count(1) as cons
from sc
group by cid
having count(1) > 5;

# 38检索至少选修两门课程的学生学号 

select sid,count(cid)
from sc
group by sid
having count(1) >= 2;

# 39查询选修了全部课程的学生信息 

select b.*
from sc a
inner join student b
on a.sid = b.sid
group by sid
having count(1) = (select count(1) from course);

# 40查询各个学生的年龄,只按年份来算 

-- 【分析】使用日期函数进行相减 
select 
    *,
    year(now())-year(sage) as age
from student;

select 
    *,
    year(curdate())-year(sage) as age
from student;

# 41按照出生日期来算,当前月日 < 出生年月的月日 则年龄减一 

-- 【分析】日期相减函数timestampdiff(year,参数1,参数2)
select
    *,
    timestampdiff(year,sage,now()) as age
from student;

# 42查询本周过生日的学生 

-- 【分析】使用week()函数
select *,week(sage),week(now())
from student
where week(sage) = week(now());

# 43查询下周过生日的学生 

select *,week(sage),week(now())
from student
where week(sage) = week(now())+1;

# 44查询本月过生日的学生 

-- 【分析】使用month()函数
select *,month(sage),month(now())
from student
where month(sage)=month(now());

# 45查询下月过生日的学生 

select *,month(sage),month(now())
from student
where month(sage)=month(now())+1;

精彩评论(0)

0 0 举报