面试题-2017/6/19

来源:互联网 发布:电脑截动图用什么软件 编辑:程序博客网 时间:2024/06/12 01:51

这是今天遇到的面试题,当时没做出来,回来查了一下开发文档,感觉不是很难

SQL语句:SELECT courseid, coursename,score,IF(60<=score,'pass','nopass') as mark from course ;

在我本机亲测:

原表



查询的到的表:





我突然想加一个需求,例如分数大于90但是小于等于100,mark显示为优;分数大于80但是小于90,mark显示为良;分数大于70但是小于80,mark显示为中;

分数大于60但是小于70,mark显示为及格;小于60显示不及格。现在SQL语句怎么写?


SELECT courseid, coursename,score,
case  when score<60 then '不及格' 
when (60<=score and score<70) then '及格'
when (70<=score and score<80) then '中'
when (80<=score and score<90) then '良'
else  '优' END
as level from course;


原表:


 查询得到的表:



参考:

as==alias 别名 (给列添加别名或者给表添加别名) 

https://dev.mysql.com/doc/refman/5.7/en/problems-with-alias.html                            ------(别名的使用)

https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#operator_case   ------(SQL控制流程功能)


2017-6-19