sql学习笔记I

union的用法

select a.s_id,a.s_name,round(avg(b.s_score),2) avg_score from
student a 
join score b on a.s_id = b.s_id 
group by a.s_id
having avg_score < 60
union
select a.s_id,a.s_name, 0 avg_score from
student a 
where a.s_id not in (select distinct s_id from score)

利用多张表联系之后筛选

select a.* from student a
where a.s_id not in(
  select s_id from score where c_id in(
    select c_id from course where t_id in (select t_id from teacher where t_name = '张三')))

结果:

s_ids_names_births_sex
06吴兰1992-03-01
08王菊1990-01-20

抽取前三名

SELECT a.s_id,a.c_id,a.s_score,b.s_score FROM score a
LEFT JOIN score b on a.c_id = b.c_id and a.s_score<b.s_score
GROUP BY a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3
ORDER BY a.c_id,a.s_score DESC
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值