ifnull(a,b)函数解释:
如果value1不是空,结果返回a
如果value1是空,结果返回b
select ifnull(写好的sql,null);
现在有“课程表”,记录了学生选修课程的名称以及成绩。
现在需要找出语文课中成绩第二高的学生成绩。如果不存在第二高成绩的学生,那么查询应返回 null。
select ifnull((select max(distinct 成绩) from 成绩表where 成绩<(select max(成绩) from 成绩表 where 课程='语文')and 课程='语文'),null) as '语文课第二名成绩';
用limit也可以,注意前面要用distinct.
select distinct 成绩 from 成绩表where 课程='语文'order by 课程,成绩 desclimit 1,1;
查询第二高的工资:
select ifNull((select distinct salaryfrom Employee order by Salary Desclimit 1,1),null) as SecondHighestSalary;