MySQL高阶2072-赢得比赛的大学

目录

题目

准备数据

分析数据

总结


题目

纽约大学和加州大学之间举行了一场比赛。这场比赛由两所大学中相同数量的学生参加。拥有更多优秀学生的大学赢得这场比赛。如果两所大学的优秀学生数量相同,则这场比赛平局。

优秀学生是指在考试中获得 90% 或更高成绩的学生。

返回:

  • "New York University" 若纽约大学赢得这场比赛。
  • "California University" 若加州大学赢得这场比赛。
  • "No Winner" 若这场比赛平局。

准备数据

Create table If Not Exists NewYork (student_id int, score int)
Create table If Not Exists California (student_id int, score int)
    Truncate table NewYork
    insert into NewYork (student_id, score) values ('1', '90')
    insert into NewYork (student_id, score) values ('2', '87')
    Truncate table California
    insert into California (student_id, score) values ('2', '89')
    insert into California (student_id, score) values ('3', '88')

newyork表

california表

分析数据

with t1 as (
    select count(student_id) c1 from newyork
    where score >= 90
),t2 as (
    select count(student_id) c2 from california
    where score >= 90
)select
     case
         when c1 > c2 then 'New York University'
         when c1 < c2 then 'California University'
         when c1 = c2 then 'No Winner'
    end winner
 from t2,t1;

总结

判断多类型,可是使用case when

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值