SQL问题

10 篇文章 0 订阅

1、行转列问题

使用case when  分组如:

select student_id ,
max(case course_id when '1' then score else 0 end ) as '语文',
max(case course_id when '2' then score else 0 end ) as '数学',
max(case course_id when '3' then score else 0 end ) as '英语'
from sc 
group by student_id

2、查询所有的

双重否定等于肯定  not exists 如:查询选修了所有课程的同学

select * from student s where not EXISTS 
    (select * from course c where not exists 
            (select * from sc  where sc.course_id = c.course_id and s.student_id = sc.student_id  )
    )

3、当表当中关联列有重复时候 但是我们只要其中一行的值的时候

可以使用分组  然后去最小的主键

select * from pg_class where oid in (select min(oid)from pg_class group by relname)

4、所有分数都大于多少的

select * from student where student_id in(
select student_id from sc group by student_id HAVING Min(score)>80 )

5、查询出选了李四所选所有课程的学生信息

双重否定,另外李四的课程
select * from student s
 where not  exists 
    #查询出李四的所有课程
    (select * from (select sc.* from sc ,student where student.student_id = sc.student_id and student.student_name='李四' ) c 
        where not exists 
        (select * from sc  where sc.course_id = c.course_id  and s.student_id = sc.student_id   )
    )
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值